Xcode storyboard switch - objective-c

Someone help me understand the new storyboard in Xcode 4.2?
I know how to code to load another view controller but in the storyboard mode there are differences..
I also know there are a lot of tutorials about the navigationcontrollers, but I just want to switch UIViewControllers on storyboard.
With the normal .xib files I can switch views with this code from the RootViewController..
SecondViewController *Second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:YES];
When I use it in the storyboard mode it just loads the UIAlertView on the SecondViewController.m and the screen appears to be black?
Any help would be appreciated, also attached the Xcode project...
Here is the zip..
-x- Jay Ruben

you can do this:
SecondViewController *second= [self.storyboard instantiateViewControllerWithIdentifier:#"second"];
[self presentModalViewController:second animated:YES];
Don't forget to give the second view controller an identifier like "second".
Otherwise you can connect both view controllers with a segue. Hold CTRL an drag from the first to the second view Controller. Now you can choose "push" and give the segue a name to switch the View programmatically like this:
[self performSegueWithIdentifier:#"second" sender:self];
Push Segues will only work if a navigation controller is set.

You can also switch this way:
// get the view that's currently showing
UIView *currentView = self.view;
// get the the underlying UIWindow, or the view containing the current view
UIView *theWindow = [currentView superview];
UIView *newView = aTwoViewController.view;
// remove the current view and replace with myView1
[currentView removeFromSuperview];
[theWindow addSubview:newView];
// set up an animation for the transition between the views
CATransition *animation = [CATransition animation];
[animation setDuration:0.5];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[theWindow layer] addAnimation:animation forKey:#"SwitchToView2"];
Download the sample project here.

Related

Add new view controller and push on that in cocoa application

I am new to OS X application. In iOS there is methods like :
1. self.window.rootViewController = self.viewController;
2. [self.window addSubview:self.viewController.view];
to add view controller in window or
3. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:#"DefaultViewController" bundle:nil];
[self.navigationController pushViewController:objDefault animated:TRUE];
4. DefaultViewController *objDefault = [[DefaultViewController alloc] initWithNibName:#"DefaultViewController" bundle:nil];
[self presentViewController: objDefault animated:TRUE completion:nil];
to push on next view controller.
My question is that in OS X is there any method like above to add new view controller to window or push on next view controller..?
Cocoa and Cocoa-touch have a little different ways to change views on the window.
To change views you need to programmatically remove old subview and add new one.
- (void)addNewSubview:(NSView *)view // NSWindowController subclass implementation file
{
[_subview removeFromSuperview];
NSView *contentView = (NSView *)self.contentView;
[contentView addSubview:view];
_subview = view;
}
or simple [contentView replaceSubview:_subview with:view];
Since OS X 10.10 there's an opportunity to use storyboards something like in iOS.
UPD
To awake window from the application delegate class create your NSWindowController-subclass instance and show the window (it's mostly like in iOS' before-storyboards era).
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CustomWindowController *controller = [[CustomWindowController alloc] initWithWindowNibName:#"CustomWindowController"];
[controller showWindow:nil];
[controller.window makeKeyAndOrderFront:nil];
}
If you are using storyboards and segues to show a new NSViewController, then you might need to close the old view controller. Here is how I do it:
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender
{
[self.view.window close];
}
Each view controller itself creates a window, so you need to close the old one before showing the new one.

Why does my Navigation Bar temporarily disappear when I dismiss a modal view in iOS 7?

When I'm going back from my Modal View Controller to my Main View Controller (I have a horizontal animation) my Main Controllers navbar places itself a bit too high for a quick second and then jumps back to its right position. Does somebody know why? Ive been googling it but with no success.
App Delegate:
[navigationController.navigationBar setBarTintColor: [UIColor whiteColor]];
[navigationController.navigationBar setTranslucent: NO];
When i push button to open my Info View:
UIViewController *infoViewController;
infoViewController = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle: nil];
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController: infoViewController animated: YES completion:nil];
I'm not using Auto Layout on any xib-files. My Main View Controller xib-file is empty with Status Bar: Default. My Info View Controller xib-file has some stuff in it.
Code for closing my Modal View Controller:
-(IBAction)onBackBtnClick:(id)sender
{
[self dismissModalViewControllerAnimated: YES];
}
All what you have to do is to add the following code in the ViewWillAppear of the "InfoViewController" viewController class
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setTranslucent:NO];
[self.navigationController.navigationBar.layer removeAllAnimations];
}
Hope it worked with you :)
The problem seems to be
infoViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
If you change this to
infoViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
then it will no longer jump. This worked for me. Good luck!

use prepareForSegue for button

Hi I create button programmatically and I connect my button to another view, but I got segue problem
that I should use prepareForSegue method for storyboard but I don't know how there ar esome sample to the internet but I will get error when I used that sample, would you please help me
Thanks in advance!
here is my code
Creating Button
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor colorWithRed: 201.0/255.0 green: 201.0/255.0 blue:201.0/255.0 alpha: 1.0];
button.tag = currentTag;
currentTag++;
[button.layer setBorderColor: [[UIColor blackColor] CGColor]];
[button.layer setBorderWidth: 1.0];
[button setTitle:[NSString stringWithFormat:#"%d",currentTag] forState:UIControlStateNormal];
button.frame = CGRectMake(80*x, 32*y, 80, 32);
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
Action for button
-(void)buttonPressed:(UIButton *)button
{
NSLog(#"button %u -- frame: %#", button.tag, NSStringFromCGRect(button.frame));
WeekView *crtObj=[[WeekView alloc]initWithNibName:#"WeekView" bundle:nil];
[self.navigationController pushViewController:crtObj animated:YES];
[crtObj release];
}
Prepare for segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"WeekView"]) {
// Get reference to the destination view controller
WeekView *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object]; //// I don't know what should I write instead of object
}}
error
Edit 2:**** I have a warning :
Segues initiated directly from view controllers must have an identifier for use with -[UIViewController performSegueWithIdentifier:sender:]
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"WeekView"]) {
// Get reference to the destination view controller
// WeekView *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
// [vc setMyObjectHere:object];
[segue.destinationViewController setTitle:#"WeekView"];
}}
There are really two ways to push a view controller onto the navigation controller.
The old way, all in code. That is what you did. You can completely forget about segues at this point.
The new way: establish a Segue in Storyboard (push segue from one view controller to a new view controller, which you also define in Storyboard), give it an Identifier name and then use this code to push the view controller.
.
[self performSegueWithIdentifier:myIdentifyer sender:self];
where self is the view controller. Now you can do the customizations in prepareForSegue:.
the sample code in this case is just telling you the type of thing you can do in prepareForSegue:sender: .
it is suggesting that the viewController to which you are attempting to segue would have a method setMyObjectHere: and it would be expecting you to pass that object.
as an example, your class might have a setTitle: method, and in the prepareForSegue:sender: code, you could pre-set the title based on information in the controller you are performing the segue from.
to get rid of the warning, you need an identifier, as in the picture below:
"The old way to code", no the better way to code hopefully this helps, but if your using UIButtons and UIViews programmatically you shouldn't even be using segues.
Create a root view control in app delegate, than just pop on and off the next viewcontrollers and such.
For example this is all you need in your viewcontrollers:
-(void)nextControllerButton:(UIButton*)button{
SearchViewController *nextController = [[SearchViewController alloc] init];
nextController.username = _username.text;
nextController.password = _password.text;
[self.navigationController showViewController:nextController sender:self];
}

Animate loaded subview

How would I make this code animate in the SplashView NIB instead of just making it appear (e.g. the UIModalTransitionStyleFlipHorizontal style)? I am using a UITabBarController type project.
- (IBAction)showSplash:(id)sender {
// Hide toolbar
self.tabBarController.tabBar.hidden = YES;
// Splash
[[NSBundle mainBundle] loadNibNamed: #"SplashView" owner: self options: nil];
[self.view addSubview: splashView];
[window makeKeyAndVisible];
}
Bit hard to tell your context with this small bit of code. Basically, if you want to push a viewController modally, in your -(IBAction)showSplash method (you don't need to send the sender if you're not using it, BTW), I would use some code similar to this:
SplashViewController *svc = [[SplashViewController alloc] init]; (assuming nib is same name)
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:svc animated:YES];
[svc release];
Then in your SplashViewController you would have an IBAction that calls:
[self dismissModalViewController animated:YES];
You don't actually have to hide the tabBar when you are presenting a modalViewController. It won't be there. The idea of a modalViewController is that it blocks all user interaction with the app except for the modal view, until it is dealt with.
Hope this helps.

UIView transition and animation

I understand modal views cover the entire screen. But I really want a view that covers only half the screen just like the keyboard. So, please tell me why this doesn't work
MyController *controller = [[MyController alloc] initWithNibName:#"MyView" bundle:nil];
CGRect frame = CGRectMake(0,44,768,264);
[controller view].frame = frame;
contoller.delegate = self;
[[self view] addSubView:[controller view]];
[controller release];
I am trying to add a sub view to my current view and make it appear where the keyboard appears.
It throws a BAD ACCESS exception
In my code (above), I was using a custom UIViewController with it's own view [set to UIView on IB]. I couldn't get it to work by setting frame for the view controller's view.
So I added a custom UIView without a Nib file with all the controls (buttons, textfields) added on initWithFrame.
MyCustomView = [[MyCustomView] alloc] initWithFrame:frame delegate:self];
[self.view addSubView:MyCustomView];
Thanks for your comment, Jacob.