Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

iphone - How can I manually switch between UIViewControllers in storyboard?

All I need is to view a UIView controller in same storyboard file manually with code. I use storyboard to make all forms and connections. My application starts in navigation controller, which provides me access to UIView (LoginViewController) and then it goes to tab bar controller, which provides 4 UIViews. According to every UIView I have .h and .m files. I know about segue method, it is simple, but I need manual method. Maybe I am doing something wrong.

I was trying to use this method for pushing view controller in IBAction:

[self.view pushViewController:LoginViewController animated:YES];

But it makes an error:

Unexpected interface name ‘LoginViewController’: expected expression

It took a lot of time to figure out what is wrong, but I had not succeed. Here is my RollEnemyController.m file:

//  RollEnemyController.m
#import "RollEnemyController.h"
#import "LoginViewController.h"
@implementation RollEnemyController;
@synthesize AttackButtonPressed;

- (IBAction)AttackButtonPressed:(id)sender {
    LoginViewController* controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController"  bundle:nil];
    [self.view pushViewController:controller];
}

@end

And this is header file:

//  RollEnemyController.h

#import <UIKit/UIKit.h>

@interface RollEnemyController : UIViewController

- (IBAction)RollButtonPressed:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *AttackButtonPressed;

@end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm guessing that you are using a UINavigationController. Then you can simply do like this:

LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];

Update:

If you are using a UIStoryboard, you can set the identifier of your new viewcontroller, and then push it onto your navigationController. To set the identifier, choose your view, open the Attributes Inspector, and set the identifier ("LoginIdentifier" in my example). Then you can do this:

LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];

As a sidenote, I see that you are using capital characters for your methods. You should probably try to avoid that, and instead use lowered first-characters in your method names. And since you say you are learning Objective-C, you should check out this awesome thread here on SO: link.

Update 2:

Here is a zip file with a project showing how to do this. :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...