performSelector:withObject:afterDelay not working inside viewDidLoad method - objective-c

I have a tabbar application, the problem is that i need to call a method after a delay of viewDidLoad of the first view but it didn't work (the method is not called)
i added the following sample
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(foo) withObject:nil afterDelay:1];
}
-(void)foo
{
NSLog(#"foo!");
}
the strange thing is that this work with all other tabs view but for some reason it didn't work with the first tab (UITableViewController)
any idea??
Thanks

try this....
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:#selector(foo) withObject:nil afterDelay:0.5];
});
}

Related

Cause viewDidLoad to run

I'm creating a new UIViewController but waiting a few seconds before I'm showing it.
The thing is I want viewDidLoad to be called before I'm actually showing the now object. How can I make viewDidLoad run?
Currently I'm doing something like this:
UIView *tempView = newObject.view;
But I really don't like that solution.
If there is code that you want to run that you want to be called when the view is instantiated then just put it in the init method.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName: nibNameOrNil
bundle: nibBundleOrNil];
if (self)
{
// Custom stuff
}
return self;
}
Is there another reason why you are wanting viewDidLoad to be called?
Looks like just calling [newObject view]; does the trick!
- (void) viewDidLoad {
[super viewDidLoad];
self.view.alpha = 0; //When alpha = 0, it is also hidden
[UIView animateWithDuration:2.0f animations:^{
// Nothing to animate here
} completion:^(BOOL finished) {
// On animation completion show the content of the view, also animated.
[UIView animateWithDuration:0.5f animations:^{
self.view.alpha = 1;
}];
}];
}
The viewDidLoad is always called before the object is shown in screen.
If you want some delay , you can set some delay code inside viewDidLoad or viewDidAppear. But why exactly do you need the delay , there may be other solutions to solve your issue.

UITableView reloadData with performSelectorInBackground

An App interface has an UISegmentedControl on the top of screen and a UITableView of the rest of the screen.
When UiSegmentedControl gets a click then NSFetchedController gets an update for NSPredicate of NSFetchRequest and perform a performFetch method.
Because, it's take a 4 seconds to take a result I would like to reload a tableview to display 0 sections and after the result is ready then reload a tableView.
how to do it ?
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelectorInBackground:#selector(exercisedata) withObject:nil];
}
-(void)exercisedata
{
[self performSelectorOnMainThread:#selector(ProcessComplete) withObject:nil waitUntilDone:NO];
}
-(void)ProcessComplete
{
[tbl reloadData];
}

Making a UIViewController a UIImagePicker when tapped

I have an app that uses the UITabBarController and one of the tabs is supposed to be a full on camera view (similar to Instagram). When I launch the app and go to that view, most of the screen is blank and no image library picker/camera view loads. Any help would be greatly appreciated.
Here is my viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
imagePicker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
[imagePicker setDelegate:self];
[imagePicker setEditing:NO];
// Place image picker (camera viewing angle on the screen)
[self presentViewController:imagePicker animated:YES completion:nil];
}
Try putting that code in viewDidAppear instead -- that worked for me. Better yet, would be to put all but the last line in viewDidLoad, so it's only called once, and just put the last line in the viewDidAppear method. When you cancel the image picker (the only thing I tested), it will just reappear, since viewDidAppear will be called again, so you need to implement imagePickerControllerDidCancel: like this to keep that from happening:
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
self.imagePicker = nil;
}
And, in your viewDidAppear, put in an if statement to see if the picker exists before trying to present it:
if (self.imagePicker) {
[self presentViewController:self.imagePicker animated:YES completion:nil];
}

viewDidLoad issue

I am having problem with ViewDidLoad Method.I have one .Xib File in my app and 5 viewControlelr.And Each View Controller Contains some initialization and method calls.
Problem is at the startup all the ViewDidLoad method runs.
Is there any way i can do this just when my ViewController will be called and the View is Loaded on the screen?
What is the proper way in this case?
I tried like this code :
-(void)viewDidAppear
{
[ScoreWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://smwebtech.com/Pandit/web_service/getScore.php?u=aa"]]];
}
But my program is not reaching this method.Why?Am i missing something?Please take a look.
Use viewWillAppear method instead. It is only called when the view is going to appear on screen.
Or viewDidAppear which is only called after the view appears on screen.
EDIT:
Both viewWillAppear and viewDidAppear take a BOOL argument each:
- (void)viewWillAppear:(BOOL)animated {
}
- (void)viewDidAppear:(BOOL)animated {
}
EDIT2:
These two delegate methods have their corresponding 'opposite' methods that you can override to release resources you allocate in the former methods:
- (void)viewWillDisappear:(BOOL)animated {
[resource release];
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[resource release];
[super viewDidDisappear:animated];
}

UINavigationController navigationBarHidden

Hey everbody, i'm having problem with a simple question.
I have a NavigationController, 01.xib ans 02.xib.
I set in IB NavigationController to dont display the navigationBar. Well, when i go to 02.xib, i set it to appear.
[self navigationController].navigationBarHidden = NO;
Everything works fine.
But, when i come back to 01.xib with the top button, the bar still appearing in the 01.xib.
How can i fix that?
Thanks!
Use this in first view controller:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:animated];
}
I believe this is happening because they are referencing the same navigation controller.
You could set it hidden again in the viewDidAppear method of your 01 class.
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self navigationController].navigationBarHidden = YES;
}