strange loading / not loading viewController - iphone

When I use this line of code in the viewDidLoad of the app's main viewController, it is just getting completely ignored:
[self presentModalViewController:nextController animated:YES];
The nextControler doesn't load, nothing at all happens.
I do know the code is being "executed" since I put an NSLog before and after it and also ran through the program with a breakpoint.
When I call this line elsewhere in the code, it works perfectly as expected, but in viewDidLoad it is being ignored.
So I tried this code to see if there would be any difference:
[self.view addSubview:nextController.view];
This is loading the nextController view over top of my main view but some of it is transparent so that the main view shows through, and when I try to click on a button in the nextController, it is actually NSLog'ing as a press on a button in the main view below it.
Does anybody have any idea what might be happening here?
Thanks in advance!

viewDidLoad is called before the view controller is inserted into the view controller hierarchy. That's why it just doesn't know how to present a modal view controller at this point. Try moving the code into viewDidAppear:.

Related

UISearchDisplayController's table view separator lines glitch when loading inside a modal view, how do I fix it?

I'm trying to load a basic search view overlaying my navigation controller (that is, not pushed on the navigation stack). To achieve this I'm using [self presentModalViewController:vc animated:NO].
Full modal view presentation code:
- (void)searchButtonPressed
{
TMSearchViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"Search"];
[self presentModalViewController:vc animated:NO];
vc.searchDisplayController.searchBar.delegate = self;
}
After the modal view is presented on the screen, the following code—inside the search view's controller—is run:
- (void)viewDidAppear:(BOOL)animated
{
[self.searchDisplayController.searchBar becomeFirstResponder];
[self.searchDisplayController setActive:YES animated:NO];
}
The problem is that when the search view renders, it does the normal thing where it blacks out the table view with a transparent black view, but it doesn't appear to cover the table view's separators, which looks like this:
I really have no idea how to fix it. My best guess is that it is something to do with the modal controller's process of loading a view that I'm not understanding properly. I've tried moving the becomeFirstResponder to the viewDidLoad method. I've also tried reordering the setActive method and the becomeFirstResponder method whilst changing the setActive method to animated:NO and YES.
So far, no avail, I'm clearly not understanding something here and I'm guessing I'm using something in a way that is unintended. Please help, Thanks :)
edit: interestingly enough, when I click the blacked out table view to resignFirstResponder the searchbar, and then click inside the searchbar again to activate becomeFirstResponder it loads fine, without the glitch, how it's meant to.
Did you try self.searchDisplayController.searchResultsTableView.separatorStyle=UITableViewCellSeparatorStyleNone; ??
Okay so after hours of messing around in the code, trying to understand what I'd done wrong, I've finally found the answer, or at least a fix. (I'm still mystified as to what was actually going wrong)
I simply deleted the searchbar from the interface builder in storyboard. And replaced it with another one—which involved reconnecting the searchbar to be the searchDisplayController's searchbar property, of course.
My best guess is that Xcode for whatever reason didn't like the order in which I dragged in and connected the UI elements to interface builder/storyboard.
Hope this helps someone.

what happens between viewWilldisappear and viewDiddisappear?

This is a follow up on a previous unsolved post.
I have a navcontroller, a tableview controller and a searchbar in the tableview. All this is built in storyboard.
I then build another viewcontroller programmatically when a cell is selected.
I do not want the navigation bar to appear in this last view so, in viewWillDisappear I call
[self.navigationController setNavigationBarHidden:YES animated:animated];
This works just fine IF I select a cell in the main tableView.
However, if the cell is selected after narrowing the data from a search in searchbar, then, the navigationbar will appear in the subsequent view although the call to setNavigationBarHidden is made in viewWillDisappear.
I tried repeating this call in viewDidDisappear and, now, the navigation bar disappears from the subsequent view although it is briefly displayed.
I would like to know what happens between the calls to viewWilldisappear and viewDiddisappear that apparently resets the NavigationBarHidden property?
This is what I found: The NavigationBarHidden property is apparently reset because when the searchbar leaves the view, it will send the navigationbar again on the view.
I am not sure I understand exactly what is happening, but this line of code in my viewWillDisappear solves the problem:
self.searchDisplayController.active=NO;
followed by:
[self.navigationController setNavigationBarHidden:YES animated:animated];
Of course, I would love to hear from someone who really understands what happens here.

iOS 5 : -viewWillAppear is not called after dismissing the modal in iPad

I am presenting modal using the following code :
AddName *add = [[AddName alloc] initWithNibName:#"AddName" bundle:nil]
add.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalView:add animated:YES];
And After my work I use following code to go back on my main view.
[self dismissModalViewControllerAnimated:YES];
So it use to call -viewWillAppear by default.
My problem is that,
It was working fine with iOS4.3.
But Its not working with iOS5.
What should I do ? Or Is that any bug in iOS5?
-viewWillAppear is only guaranteed to be called in places where the -viewWillDisappear has also been called. For most modal windows on the iPad, this is not the case, since they don't obscure the entire page.
The solution to your problem will depend on what you need the -viewWillAppear for, but in general, you're likely to need to make a call directly from the same place that you dismiss the modal view controller.
One common mechanism for this, especially in cases where you might use that same modal view somewhere else, is to give the modal view controller a delegate which is called when the view is about to disappear. This will give you a chance to take the responses from the modal window, or even just force a data reload in the delegate view.
Hope this helps.
iOS 5 definitely changed their calls to viewWillAppear and viewWillDisappear. For instance, subviews (View Controller's views as subviews to be exact) in a UIScrollView, viewWillDisappear will get called when you push another view controller onto the stack. However, when the view controller is popped, viewWillAppear does not get called. These methods were never called in iOS 4 on UIScrollView subviews.
This is strange behavior to me. Couple that with the fact that regardless of what should happen, if you could rely on it happening in iOS 4, it should not be working differently in iOS 5. Most of the time, I have no idea in which particular instance each one is called, I usually trial and error it as I'm in the zone coding. If it works the way I like, I move on. Then iOS 5 comes in and throws a wrecking ball into everything.
I have also experienced when a UINavigationController's view is a subview, and a ViewController is pushed on the navigation controller stack, viewWillAppear never gets called in iOS 4, but does get called in iOS 5. Go figure.
I had the same problem.
I found that viewWillAppear isn't get called after dismissing modal but viewDidAppear is. So just try viewDidAppear instead.

black bar as the view fades when I am using navigation controller

I'm writing my first iPhone app and I am trying to figure out how to have a MasterView and DetailsView like in the example. However, instead of using a TableView, I want to use a button on the MasterView to go to the SignUpView. I want the MasterView to NOT have a navigation bar but the SignUpView needs to have one.
I have tried putting a NavigationController into the MasterView using the interface builder. This doesn't seem to do anything at all ... I.e. I make the following call:
[self.navigationController pushViewController:signUpViewController animated:YES];
And nothing happens. The SignUpView is never shown.
So then I declared a NavigationController in the AppDelegate. The above call in the same function that it was in before (button handler, button is in MasterView) works now! It takes me to the SignUpViewController.
however, the issue is, when I press back on the navigation bar in the sign up view, the navigation bar shows up again in the MasterView. I tried to set
self.navigationController.navigationBarHidden = YES;
in viewDidLoad and viewDidAppear, but that causes a black bar to appear in the transition from SignUpView to MasterView.
I tried to not set it in one of the two, and that causes the animation to go smoothly, but the navigation bar shows up in the MasterView.
I feel like this should be pretty simple to do ... but I'm at my wits end trying to figure this out. Some help would be really appreciated!
Thanks.
Probably not the answer to your question, but just a small suggestion. In the many apps that I have come across, a sign-up/sign-in view is generally displayed as a modal view (on top of your master view) with a 'cross' in the top-right corner to dismiss it. Probably it results in a better user experience.
Also, did you try self.navigationController.navigationBarHidden = YES; in the MasterView's viewWillAppear ?
HTH,
Akshay
I had this problem too, until I discovered setNavigationBarHidden. You will probably want to use these in viewWillAppear/viewWillDisappear or viewDidAppear/viewDidDisappear. You don't want to call this in viewDidLoad because that is only called once when the view is initialized, not every time it appears.
To hide:
[self.navigationController setNavigationBarHidden:YES animated:YES];
To show:
[self.navigationController setNavigationBarHidden:NO animated:YES];

How can I add a ViewController to scrollview

I have a scrollview I'm trying to add ViewControllers to from a nib file.
ViewController *controller=[[ViewController alloc] initWithNibName:#"ViewController" bundle:[NSBundle mainBundle]];
[scrollview addSubview:controller];
Scrollview is defined elsewhere.
Whenever I add other items like UIViews with simple colored backgrounds, that works fine. When I add the ViewController from the nib, the Viewdidload gets hit, the images load from the nib, but then the controller code stops there (No viewWillAppear etc). So I don't get any of the controller's processing.
This same controller works fine when pushed as a nav or tab controller. I'm sure I'm missing something in my educational process, but was hoping someone could help out so I can move on.
BTW: The ViewController above is a simple UIViewController.
I think your issue is that addSubview is expecting a UIView not a UIViewController - you probably get a warning when compiling ('may not respond to selector' or similar?). If so, don't just ignore those - in my experience, usually they are a problem in your code :-)
You could try something like
[scrollview addSubview:controller.view];
But this still won't get you the viewcontroller's processing ...you need to do that processing in the view controller that has the scrollview AFAIK. I would suggest trying to get a basic view with a scrollview working in IB first.
OK, seems a little off, but I added viewWillAppear to viewDidLoad, and that worked, but this doesn't seem the right way to do it, correct?