Why the above statement is not being executed first - ios

i have used master-detail view in my application.
when videos row is selected in master view , the videos method is called and it is as shown above, in the videos method i am writing code to dismiss the popover and calling a method fetchvideosfromurl: , but during execution the method fetchvideosfromurl is being executed first and after that the popover dismisses, but i want the popover to be dismissed before the method is called, how to do it. Thank you

you can do the following:
-(void)videos
{
[popover dismissPopoverAnimated:NO];
[self performSelector(loadUrl) withobject:nil afterDelay:2.0];
}
-()loadUrl
{
//code in the following Image
}
hope this will help you.

Related

iOS presentViewController doesn't invoke viewDidLoad

I'm implementing my own 'back' button. Where onClick, the following code is executed in the ViewController (VC) being dismissed:
Dismiss current VC (VC#1)
Pop current VC (VC#1) off my custom navigationStack
Get the last VC (VC#2) from the navigationStack, and present it using
presentViewController
What happens is the back works visually works - i.e. current VC disappears, previous VC appears. However, the viewDidLoad method is not called. So the screen isn't updated with data updates from viewDidLoad.
[self dismissCurrentViewController:self completion:^{
[TWStatus dismiss];
FHBaseViewController *vcToDisplay = [[FHDataManager sharedInstance] popNavigationStack];
[vcToDisplay.homeVC presentViewController:vcToDisplay animated:NO completion: ^{ }];
}];
Questions:
I was under the impression that viewDidLoad always gets called when presentViuewController is used??
I 'build' the screen using a method called ONLY from viewDidLoad in VC#2. How is iOS displaying the screen without coming into viewDidLoad?
btw, I'm not using storyboards. Any help is appreciated!
My guess is that viewWillAppear is being called but viewDidLoad is not, at least not when you expect it is. viewDidLoad should be called once, but depending on how you're managing the view controllers, viewDidLoad may not be triggered every time your view appears (which happens after loading).
The completion handler is called after the viewDidAppear: method is called on the presented view controller. from presentViewController doc
so put this in your code with a breakpoint on the call to super and verify it is getting called when this transition occurs.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
edit: since you verified that viewWillAppear is getting called, then I would say that it's coming down to how you are managing the view controller life cycle. Even with a standard UINavigationController, viewDidLoad is not called when a view is shown as a result of popping items on the navigation stack. I would move your logic to viewWillAppear if you are dead set on not using UINavigationController
When I make a back button pragmatically I use:
[self.navigationController popViewControllerAnimated:YES];
This will invoke the viewDidLoad method. Use that instead of your current code.

Call Delegate Method Prior to View Dismissal

I have a modal view controller presented and just before I dismiss it, I need to call a delegate method which tells the parent view controller to update. (As methods like viewWillAppear are not called when dismissing a modal view controller).
So my code looks like this:
[delegate addEquipmentDidSave:YES];
[self dismissViewControllerAnimated:YES completion:nil];
Very simple. Send a message back, saying, update now! And then just dismiss the view. However, while both of these lines are called, the delegate method never runs. So I check that the delegate it set correctly. When I present the modal view I set the delegate, so its all connected.
Its as if the delegate method isn't getting a chance to run before the view is dismissed. Is this possible? What do you think might be the issue?
Thanks.
Before calling your delegate method first check whether it's available or not
if ([self.delegate respondsToSelector:#selector(addEquipmentDidSave:)] )
{
NSLog("Yes it's available");
[self.delegate addEquipmentDidSave:YES];
}
[self dismissViewControllerAnimated:YES completion:nil];
Do you see that last parameter, the one called completion? That block is called after the view controller is dismissed. Do what you want to do in there.

Displaying the right UIView when dismissModalViewControllerAnimated:completion: is called

Let me explain. I have multiple UIViewControllers. On my MainPageController, I have 3 UIViews. Let's enumerate it this way: the first UIView is called LoginView, the second is called HomeView and the other one is called RegView. Now in HomeView, there are multiple buttons that will lead to other UIViewControllers. For example, one button will lead to StoreController. Now if I am inside StoreController and I want to go back to MainPageController, I simply call:
[self dismissModalViewControllerAnimated:YES completion:nil]
This will send me back to the HomeView.
That is good. However, inside the StoreController, there are buttons which will supposedly direct me to LoginView or RegView, whichever button was tapped. The problem is when the method [self dismissModalViewControllerAnimated:YES completion:nil], it only take me back to HomeView, no matter which button I pressed.
So how will I display the right UIView once the dismissModalViewControllerAnimated is called?
EDIT:
This is how I show the UIViews:
-(void)viewDidLoad
{
//Initialize the views here...
}
-(void)showViewByTag:(NSInteger)tag
{
if (tag == 1)
{
[self.view addSubview:loginView];
}
else if (tag == 2)
{
[self.view addSubview:homeView];
}
else
{
[self.view addSubview:regView];
}
}
Now I call the method showViewByTag: somewhere in my code to display the views.
What you could try and do is following: before calling [self dismissModalViewControllerAnimated:YES completion:nil] (and thus go back to your home view), change the view currently displayed in your MainPageController:
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
If you are worried at the cast and you foresee that self.presentingViewController might be not of MainPageController type on some occasions, then you can check explicitly for its type:
if ([self.presentingViewController isKindOf:[MainPageController class]])
[(MainPageController*)self.presentingViewController showViewByTag:desiredViewTag];
[self dismissModalViewControllerAnimated:YES...];
For this to compile, MainPageController.h must be imported in your modal controller class.
dismissModalViewController will always bring back the viewController which presented it ,and that can be only one,so the ideal way would be to tell the navigationController to initWith your desired viewController..
eg on regButton click in the presented modalview
RegViewController *regViewController = [[RegViewController alloc]initWithNibNam:#"RegViewController" bundle:nil];
[self.navigationController initWithRootViewController:regViewController];

How to run viewDidAppear

I need to load some data into a view every time it is shown. The data changes, each time time view is shown, so I figure I can load the data in the method viewDidAppear. Unfortunately, I've found that viewDidAppear is not called each time the view is displayed.
The code that displays the view from any other view is....
[self clearView];
[self.view insertSubview:fifthViewController.view atIndex:4];
So I figured I could change it to the following to run viewDidAppear...
[[self.view insertSubview:fifthViewController.view atIndex: 4 viewDidAppear:YES];
Unfortunaely, this causes an error "bad receiver type 'void'
What do I need to do to insert the subview and also call viewDidAppear?
If you show the view by modifying ViewController.view visibility directly, you won't get viewDidAppear message by that. You need to use ViewController method to display the view, e.g. push controller into UINavigationController or using presentModalViewController method. You can use the hack like calling viewWillAppear: and viewDidAppear: manually, but I don't like the idea.
Thank you for the assistance with this question.
I have settled on the addition of a viewDidAppear in the method that inserts the subview.
Below is the code that is working for me at this time.
In the .m file of the highest level view controller, the following code sets up the viewDidAppear call and then inserts the fifth subview.
-(IBAction) loadFifthView:(id)sender
{
[fifthViewController viewDidAppear:YES]; // sets up viewDidAppear
[self clearView];
[self.view insertSubview:fifthViewController.view atIndex:4];
}
With the above code snippet in place, the following code snippet, located in the .m file of the fifth view controller reports that it is working.
- (void)viewDidAppear:(BOOL)animated
{
NSLog(#" xxxxxxxxxxxxxxxx inside viewDidAppear ");
}

viewDidAppear running, and then not running.

In my ios app, I begin by loading the SecondViewController using the code shown below. When that view controller appears, I also run viewDidAppear as shown in the code snippet below.
That works ok. From there, I load my FifthViewController and do some calculating which needs to be returned to the SecondViewController.
Unfortunately, when I return to SecondViewController via the same exact method, the viewDidAppear method does not run.
Any suggestions on how to fix that would be appreciated.
-(IBAction) loadSecondView:(id)sender
{
NSLog(#"In loadSecondView method \n");
[secondViewController viewDidAppear:YES];
[self clearView];
[self.view insertSubview:secondViewController.view atIndex:1];
}
If you want something to be performed every time after dismissing a viewcontroller, you should use delegate method. It is much more specific than viewDidAppear or viewWillAppear.
Here's a website where you can get started.

Resources