Welcome! This is a website that everyone can build together. It's easy!

MarkelSoft iPad, IPhone and iPod Touch Coding ExamplesThis is a featured page

The following are coding examples for common tasks that a developer may encounter.


Coding examples:

1. Show network activity meter


[UIApplication sharedApplication].networkActivityIndicatorVisible = YES; // shows network activity indicator at top of mobile device
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; // hides network activity monitor


2. Sleep for a specific number of seconds


// sleep for 3 seconds
NSDate * future = [NSDate dateWithTimeIntervalSinceNow: 3.0];
[NSThread sleepUntilDate: future];

// OR
[NSThread sleepForTimeInternal:3.0];

3. Run a method on a new thread


// Start the method parseXMLFileAtURL on a new thread
NSString * path = @"http://rss.ent.yahoo.com/movies/upcoming.xml";
[NSThread detachNewThreadSelector:@selector(parseXMLFileAtURL:) toTarget:self withObject:path];

// Note: be sure to exit the thread when processing is done in the parseXMLFileAtURL method
[NSThread exit];

4. Display a wait icon when performing lengthy operations


// This example creates a bar button with an activity indicator view (wait icon).
// When you are done doing your processing set the button back to its normal button.
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(0, 0, 20, 20);
[activityIndicator startAnimating];
UIBarButtonItem activityItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[activityIndicator release];
self.navigationItem.leftBarButtonItem = activityItem;

// do your work preferably on a thread. See this example
// .......
// In the thread processing set the button back to the normal button.


5. Get a date in the future


// get a date 60 seconds from now
NSDate * future = [NSDate dateWithTimeIntervalSinceNow: 60.0];

Also see sleep for N seconds which uses this code.


6. Select a table row


// select table row 2...
int destination_row = 2;
NSIndexPath *ip = [ NSIndexPath indexPathWithIndex:destination_row ];
[ MyTableView selectRowAtIndexPath:ip animated:NO scrollPosition:UITableViewScrollPositionTop ];


7. Deselect the current table row


// deselect the currently selected table row...
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:NO];

8. Start a timer which when it expires will call a function


// select table row 2...
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(finishLoading) userInfo:nil repeats:NO];
.
.

.
-(void)finishLoading {

}







No user avatar
markelsoft
Latest page update: made by markelsoft , Sep 24 2010, 4:39 PM EDT (about this update About This Update markelsoft Edited by markelsoft

20 words added
1 word deleted

view changes

- complete history)
Keyword tags: None
More Info: links to this page
There are no threads for this page.  Be the first to start a new thread.