UiButton / IBAction - 从 RootView 链接到 Storyboard 中的 mainView
我尝试调用故事板上的主 ViewController
。在我的应用程序中,有一个额外的 .h
、.m
文件,没有 xib 或 Storyboard。
在这个 .m 文件中,T 创建了一个按钮:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(home:)forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
NSLog(@"Home-Button line 645");
这个按钮应该链接到 Storyboard 中的主 ViewController。该视图具有标识符 HauptMenu。我没有收到错误,但视图没有更改为我的主 ViewController。怎么了?
- (IBAction)home:(id)sender {
NSLog(@"Button was tapped");
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
NSLog(@"1");
[viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
NSLog(@"2");
[self.navigationController pushViewController:viewController animated:NO];
[viewController release];
NSLog(@"3");
}
I try to call the main ViewController
on my storyboard. In my app there is a additional .h
, .m
file with no xib or storyboard.
In this .m file T craeted a button:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(home:)forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
NSLog(@"Home-Button line 645");
This button should link to my main ViewController in the Storyboard. The view has the identifier HauptMenu. I got no error, but the view doesnt change to my main ViewController. What is wrong?
- (IBAction)home:(id)sender {
NSLog(@"Button was tapped");
ViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
NSLog(@"1");
[viewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
NSLog(@"2");
[self.navigationController pushViewController:viewController animated:NO];
[viewController release];
NSLog(@"3");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
.m
文件未与任何 Storyboard 关联,那么self.storyboard
不会是Nil
吗?尝试:
确保将
storyboardWithName:
更改为故事板的名称。你可能没有得到任何错误,因为 Objective-C 处理 nil 的方式与其他语言不同,如果你尝试调用 nil 上的方法,它(通常)不会抛出异常,它只会返回 nil。以下代码将愉快地运行,不会抛出编译器或运行时错误:
If your
.m
file is not associated with any storyboard, wouldn'tself.storyboard
beNil
?Try:
Make sure to change the
storyboardWithName:
to whatever your storyboard is named.You may not have gotten any errors because Objective-C handles nil differently than other languages, it (usually) won't throw an exception if you try to call a method on nil, it will just return nil. The following code will happily run, throwing no compiler or runtime errors:
我自己也遇到过这个问题。感谢这个答案,现在它可以工作了!
这是我的代码,希望其他人可以使用它:
I had this problem myself. Thanks to this answer now it works!!
Here is my code, hopefully someone else can use this: