UiButton / IBAction - 从 RootView 链接到 Storyboard 中的 mainView

发布于 2024-12-16 17:04:32 字数 1068 浏览 3 评论 0原文

我尝试调用故事板上的主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2024-12-23 17:04:32

如果您的 .m 文件未与任何 Storyboard 关联,那么 self.storyboard 不会是 Nil 吗?

尝试:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                               @"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];

确保将 storyboardWithName: 更改为故事板的名称。


你可能没有得到任何错误,因为 Objective-C 处理 nil 的方式与其他语言不同,如果你尝试调用 nil 上的方法,它(通常)不会抛出异常,它只会返回 nil。以下代码将愉快地运行,不会抛出编译器或运行时错误:

UIViewController * test = nil;
[test viewDidLoad];

NSString * storyBoardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyBoardName = @"MainStoryboard_iPad";
} else {
    storyBoardName = @"MainStoryboard_iPhone";
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                   storyBoardName bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];

If your .m file is not associated with any storyboard, wouldn't self.storyboard be Nil?

Try:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                               @"MainStoryboard" bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];

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:

UIViewController * test = nil;
[test viewDidLoad];

NSString * storyBoardName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    storyBoardName = @"MainStoryboard_iPad";
} else {
    storyBoardName = @"MainStoryboard_iPhone";
}
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
                                   storyBoardName bundle:[NSBundle mainBundle]];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"HauptMenu"];
所谓喜欢 2024-12-23 17:04:32

我自己也遇到过这个问题。感谢这个答案,现在它可以工作了!

这是我的代码,希望其他人可以使用它:

 // Declare the storyboard
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

 // Declare the next view controller
 NextViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];

 // Pass value
 nextViewController.result = result;
 [currentObject memberfunction:value];

 // Define transition
 nextViewController.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

 // Perform change
 [self presentModalViewController:nextViewController animated:YES];

I had this problem myself. Thanks to this answer now it works!!

Here is my code, hopefully someone else can use this:

 // Declare the storyboard
 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];

 // Declare the next view controller
 NextViewController *nextViewController = [storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];

 // Pass value
 nextViewController.result = result;
 [currentObject memberfunction:value];

 // Define transition
 nextViewController.modalTransitionStyle=UIModalTransitionStyleCoverVertical;

 // Perform change
 [self presentModalViewController:nextViewController animated:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文