iphone - 如何在 Storyboard中的 UIViewController 之间手动切换?

标签 iphone ios xcode uiviewcontroller ios5

我所需要的只是使用代码在同一个 Storyboard文件中手动查看 UIView Controller 。我使用 Storyboard来制作所有表格和连接。我的应用程序从导航 Controller 开始,它使我可以访问 UIView (LoginViewController),然后转到选项卡栏 Controller ,它提供了 4 个 UIViews .根据每个 UIView,我有 .h.m 文件。我知道 segue 方法,它很简单,但我需要手动方法。也许我做错了什么。

我试图使用这种方法在 IBAction 中推送 View Controller :

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

但是报错:

Unexpected interface name ‘LoginViewController’: expected expression

我花了很多时间才弄清楚哪里出了问题,但我没有成功。 这是我的 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

这是头文件:

//  RollEnemyController.h

#import <UIKit/UIKit.h>

@interface RollEnemyController : UIViewController

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

@end

最佳答案

我猜您正在使用 UINavigationController。然后你可以简单地这样做:

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

更新:

如果您使用的是 UIStoryboard,您可以设置新 View Controller 的标识符,然后将其推送到您的导航 Controller 。要设置标识符,请选择您的 View ,打开属性检查器,然后设置标识符(在我的示例中为“LoginIdentifier”)。然后你可以这样做:

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

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

更新 2:

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

关于iphone - 如何在 Storyboard中的 UIViewController 之间手动切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8348109/

相关文章:

ios - 该代码在armv7(32位)上完美运行,但在arm64(64位)上运行不佳

iphone - 如何从 4X4 imageView 矩阵创建 1 个组合图像

iphone - 这是内存泄漏吗?

android - ios opengl es 在干净的 C 上渲染纹理

objective-c - 为什么我的 UIButton 的 selected 属性设置不正确?

xcode - 在终端中使用 REPL 编写 Swift 有什么好处?

xcode - 命令/Xcode4/Platforms/iPhoneOS.platform/Developer/usr/bin/clang 失败,退出代码为 252

ios - 仅在 iPhone 4S 上在 postNotification 后添加观察者

iphone - 从 NSData 或 UIImage 查找图像类型

ios - swift 2.2 中的不同 UUID