使用IBAction切换回主视图控制器

时间:2011-04-04 23:08:41

标签: iphone objective-c xcode interface-builder

我有一个应用程序,它有许多视图,通过我的主视图中的许多自定义UIButton导航到这些视图。主视图名为iBMRViewController,并以通过界面构建​​器删除的PNG图像的形式提供一些欢迎图形。它还具有6个自定义UIButton,我通过代码使用以下代码创建;

// This is the code which creates, and defines the properties of the 'Warning' button on the main view.
    UIButton *warningButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    warningButton.frame = CGRectMake(225.0, 270.0, 60.0, 60.0);
    [warningButton setTitle:@"" forState:UIControlStateNormal];
    warningButton.backgroundColor = [UIColor clearColor];
    [warningButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
    UIImage *warningButtonImageNormal = [UIImage imageNamed:@"Warning.png"];
    UIImage *warningStretchableButtonImageNormal = [warningButtonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [warningButton setBackgroundImage:warningStretchableButtonImageNormal forState:UIControlStateNormal];
    UIImage *warningButtonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *warningStretchableButtonImagePressed = [warningButtonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [warningButton setBackgroundImage:warningStretchableButtonImagePressed forState:UIControlStateHighlighted];
    [warningButton addTarget:self action:@selector(warningButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:warningButton];

现在,一切正常,按钮功能完美,显然我也为他们设置了完美的动作。在每个页面上,我通过界面构建​​器设置了一个带有UINavigationItem的UINavigationBar,并设置为使用以下代码将我带回主视图;

//This is the code which opens up the new view when 'Begin' button is tapped.
-(IBAction)beginHomeButtonAction:(id)sender {
    iBMRViewController *controller = [[BeginView alloc] initWithNibName:@"iBMRViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release];

然而,当它返回'iBMRViewController'时,它也可以工作,它只显示通过界面构建​​器xib文件设置的内容(即welcome png文件)。它不显示我通过代码添加的按钮。

有谁能让我知道我哪里出错了?非常感谢。

由于

1 个答案:

答案 0 :(得分:0)

事实上,beginHomeButtonAction并没有“带你回到主视图”。它创建了一个具有新视图的新视图控制器,并提供了该视图。它与控制器类的相同,但不是该类的实例

要关闭视图,并且实际上将用户带回主视图,您必须关闭从主视图中显示的视图。如何执行此操作取决于您当时的呈现方式,但您可以尝试popViewControllerAnimateddismissModalViewControllerAnimated。 (谷歌是你的朋友)