From ViewController to TabBarController - uiviewcontroller

My initial VC is the LoginController. After the login a seque is going to a tabbar. This works fine but what I want is to check if the user is logged in to present the tabbar.
I put [self performSegueWithIdentifier:#"tabBar" sender:self];
in LoginController inside the viewdidAppear but is not nice because it shows the LoginView for a while. If put it inside the viewDidLoad is not working.
Any suggestions on how to solve it?

Related

How to setup a UIBarButtonItem to trigger an action in ios

I'm trying to work out how I can get my UIBarButtonItem named "home" to return back to my first initial ViewController.
I have implemented my button within the viewDidLoad of my ViewControllerSecond:
#property (nonatomic, weak) UIBarButtonItem *homeProperty;
// Create a home button
UIBarButtonItem *homeButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(homeProperty)];
[self.navigationItem setLeftBarButtonItem:homeButton animated:NO];
Do I need to create an IBAction method for this? Also, is the UIBarButtonItem implemented in the correct method viewDidLoad
So to summarise I need to be able to go back to the ViewController from the ViewControllerSecond but I can't figure out how to do this.
Many thanks
How are you adding the second view controller to the first? Are you calling pushViewController or presentModalViewController? If you're using a UINavigationController, calling pushViewController should automatically create the back button for you on the navigation bar. Create a new project in XCode using the Master-Detail Application too see an example of how UINavigationController should work.

Passing data between two views with Xcode 4.6

I am studying Object-C and I am writing a test app to learn.
This is an easy app, where in the first View i press a button, go in the second View (I used a curl animation)fill a text field and then press a button, pass the content of the field in the first view.
So I created the 2 Views, placed a button and a Label in the first View and linked the button from the first View to the second View using the right mouse click and drag a line with action Modal.
In the second View, I wrote the following code:
ViewController2.h
#property (weak, nonatomic) IBOutlet UITextField *firstTextField;
#property (strong, nonatomic) NSString *stringFromVC2;
- (IBAction)passTextToVC2Button:(id)sender;
ViewController2.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.firstTextField.delegate = self;
}
- (IBAction)passTextToVC2Button:(id)sender {
ViewController *VC2 = [self.storyboard instantiateViewControllerWithIdentifier:#"wb_ViewController"];
VC2.stringFromTextField1 = self.firstTextField.text;
[self presentViewController:VC2 animated:YES completion:nil];
}
The app works, but when I press the button from ViewController2, and go back to ViewController, it doesn't animate the Curl animation, just it appears from the bottom of the page a new window (the ViewController) under the ViewController opened with the Curl animation.
I also tried to use:
[self dismissViewControllerAnimated:YES completion:nil];
instead of:
[self presentViewController:VC2 animated:YES completion:nil];
in this case, it closes the second view but the Label in the first View isn't display with the text I inserted in the second View.
As I told you, I am studying so I am a beginner, please be patient.
The way to do this is to define a delegate protocol that your first view controller conforms to, and your second view controller can send a message to to pass back the value.
I've had to answer this question a few times so I created an very simple example project to show exactly what I mean. Download it and see how to pass data back down the controller stack.
Update
I've changed the sample to an new project for iOS6 with ARC and Storyboards.

UITabbarController dismiss modal UINavigationController

I got a very interesting problem here. My iPhone app has an UITabbarController as rootViewController in the AppDelegate.
If the app is opened the first time, it must be configured basically. For this purpose I create an UINavigationController and tell the tabbarController to present it modally:
firstRun = [[firstRunViewController alloc] init];
navCtrl = [[UINavigationController alloc] initWithRootViewController:firstRun];
[[self tabBarController] presentModalViewController:navCtrl animated:NO];
When the configuration is done, I'd like to get rid of the firstRunViewController. I'm using this technique very often, using -dismissModalViewControllerAnimated:.
But in this constellation this doesn't work. It doesn't matter from what controller I'm calling the dismiss.
I tried it via the tabbarController, the rootViewController, the currently active viewController, of cause self and several other controllers.
EVERY TIME I call -dismissModalViewControllerAnimated: I get this exception:
'UIViewControllerHierarchyInconsistency', reason: 'presentedViewController for controller is itself on dismiss for: <UINavigationController:…
Can anybody help? Thanks in advance, with kind regards, Julian
EDIT
In my AppDelegate I'm using a UITabbarController as rootViewController for the main window:
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
Then I'm creating an UINavigationController and tell the UITabbarController to present the modalViewController:
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:firstRun];
[[self tabBarController] presentModalViewController:navCtrl animated:NO];
When I now call -dismissModalViewControllerAnimated: on the firstViewController I'm getting the error from above.
In my opinion you are abusing UITabbarController. This class, even though a subclass of UIViewController, does not really use much of the UIViewController infrastructure.
What you want is a slight extension of what you have now. Create a new UIViewController subclass in your appDelegate, and add it as the single object to an array, and set the tabBar's viewControllers to this array. Set your subclass' hidesBottomBarWhenPushed to YES so it hides the tab bar when it becomes visible.
Now your app will launch and your UIViewController subclass will become the frontmost view. You can make this view the one you wanted to present modally, or you can present that view from your subclass using some kind of animation. Oh, and if you use the launch view as the background image for your subclass, you can really make this a smooth transition - I do this now.
When your modal view is done, then you can instantiate whatever views you want to then display, and set the UITabBarController to use those views with tabBarController.viewControllers (or the animated version). Poof, you UIViewController will get replaces (and under ARC just disappear).
I don't have a chance to test my hypothesis, but I suspect that this issue could depend on the fact that you are presenting the modal view too early, whereby too early means before the main window has had the chance to set up the tab bar controller. So, I would suggest this changes:
create a method to instantiate your navigation controller:
- (void)initializeAndPresentNavigationController {
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:firstRun];
[[self tabBarController] presentModalViewController:navCtrl animated:NO];
}
instead of presenting the navigation controller directly from appDidFinishLaunching, call the above method asynchronously:
[self performSelector:#selector(initializeAndPresentNavigationController) withObject:nil afterDelay:0.0];
Here the trick of calling the method as I do in 2 is that the call to initializeAndPresentNavigationController will be simply pushed on the main loop, and executed after your app has had the possibility to build its initial UI.
Hope it works for you.
I finally found the answer myself!
I just couldn't see the wood for the trees! I'm quite happy right now! :)
I did really silly things: In the last viewController of the setup viewControllers I had to change the tabars viewControllers corresponding to whether the user is administrator or not. So I did:
appDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:appDelegate.readState,
appDelegate.navCtrl,
appDelegate.settings, nil];
You can see that I was adding the AppDelegate's "navCtrl" to the tabbar's viewControllers. So I was trying to dismiss a viewController I just added to the parentViewControllers (UITabbarController) sub-controllers.
Dismissing something I want to present just in the same moment is NOT advisable! :))

UIViewController's viewDidAppear not being called after a modal close

A UIViewController (View A) invokes another view controller (View B) by invoking it as a modal control.
[self presentModalViewController:ViewB animated:TRUE];
And View B exists by invoking:
[self dismissModalViewControllerAnimated:TRUE];
When this occurs everything looks right EXCEPT that View A's viewWillAppear and viewDidAppear does not get called (they are called during app init though). Weird thing is... i believe ive done this before, but im not sure what is going on now.
Is there anything obviously wrong that im doing?
Thanks!
* UPDATE *
I just now learned that this behavior only occurs with the UIModalTransitionStylePartialCurl transition type. For all other transition types the parent view-controller gets its viewDidAppear message just fine.
So now what am i suppose to do!?!
I just ran in to the same problem.
I solved it by adding a delegate and a delegate method.
So when Controller A opens Controller B as a modal view controller with a page curl i set the instance of controller b's.delegate to be controller a.
In controller B i add this:
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (delegate)
[delegate didCloseInfoViewController];
}

Only direct subviews of the MainWindow view gets orientation set by iOS?

Whenever my app is rotated, only the viewController of which I've added his view as a subview to the mainwindow gets his interfaceOrientation property updated, the rest remains ignorant of the fact the device has been rotated.
Is it my responsibilty to notify other objects of the change, and if so, what's a nice way to do it?
I've looked into setting interfaceOrientation of my children-viewcontrollers but that's readonly.
Thanks in advance,
I found that calling
willRotateToInterfaceOrientation
and/or
didRotateFromInterfaceOrientation
of the view controller you are going to show worked for me. In my case I was using a Navigation Controller so it was easy to keep track of what was going to be shown to the user next. Below is some code from my project.
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
[viewController willRotateToInterfaceOrientation:
[self interfaceOrientation] duration:0];
}