如何在故事板中手动切换UIViewControllers?

kok*_*oko 25 iphone xcode uiviewcontroller ios ios5

我只需要UIView使用代码手动查看同一故事板文件中的控制器.我使用故事板制作所有表格和连接.我的应用程序在导航控制器中启动,它为我提供了访问UIView(LoginViewController)然后它转到标签栏控制器,它提供4 UIViews.根据每个UIView.h.m文件.我知道segue方法,很简单,但我需要手动方法.也许我做错了什么.

我试图使用此方法推送视图控制器IBAction:

[self.view pushViewController:LoginViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)

但它出错了:

意外的接口名称'LoginViewController':预期的表达式

花了很多时间才弄清楚出了什么问题,但我没有成功.这是我的RollEnemyController.m档案:

//  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
Run Code Online (Sandbox Code Playgroud)

这是头文件:

//  RollEnemyController.h

#import <UIKit/UIKit.h>

@interface RollEnemyController : UIViewController

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

@end
Run Code Online (Sandbox Code Playgroud)

mat*_*tsr 108

我猜你正在使用UINavigationController.然后你可以这样做:

LoginViewController *controller = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
Run Code Online (Sandbox Code Playgroud)

更新:

如果您使用的是a UIStoryboard,则可以设置新viewcontroller的标识符,然后将其推送到navigationController.要设置标识符,请选择视图,打开"属性"检查器,然后设置标识符(在我的示例中为"LoginIdentifier").然后你可以这样做:

LoginViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];
[self.navigationController pushViewController:controller animated:YES];
Run Code Online (Sandbox Code Playgroud)

作为旁注,我看到你正在为你的方法使用大写字符.您可能应该尝试避免这种情况,而是在方法名称中使用降低的第一个字符.既然你说你正在学习Objective-C,你应该在SO:link上查看这个很棒的主题.

更新2:

是一个zip文件,其中包含一个显示如何执行此操作的项目.:-)