简体   繁体   English

iPhone SDK:切换到一个视图然后返回到先前的视图错误

[英]iPhone SDK: Switching to one view then back to previous view errors

I have a UITabBarConroller that I use to switch between 3 different views. 我有一个UITabBarConroller,可用于在3个不同的视图之间切换。 This all works perfectly. 所有这一切都完美。 On one of my tabs, I added a button at the to called "Add", I have added an outlet to this, as well as an IBAction method which looks like the following: 在我的其中一个标签上,我在处添加了一个名为“添加”的按钮,为此添加了一个出口,以及一个类似于以下内容的IBAction方法:

// Method used to load up view where we can add a new ride
- (IBAction)showAddNewRideView {    

    MyRidesViewController *controller = [[MyRidesViewController alloc] initWithNibName:@"AddNewRide" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];
    [controller release];

}//end showAddNewRideView

This currently works fine, and loads up my AddNewRide nib file. 目前,这工作正常,并加载了我的AddNewRide nib文件。 But, once that view loads, I have a cancel button, which, when clicked, I want to return to the previous view. 但是,一旦加载了该视图,我就会有一个取消按钮,单击该按钮后,我想返回上一个视图。 So, I figured I would just do the reverse of the above, using the following method which would load back my previous nib: 因此,我想我将使用以下方法加载上面的笔尖,而做与上述相反的操作:

- (IBAction)cancelAddingNewRide {
    MyRidesViewController *controller = [[MyRidesViewController alloc] initWithNibName:@"MainWindow" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:controller animated:YES];
    [controller release];

}//end cancelAddingNewRide

But, which trying to load the MainWindow nib, the program crashes, and I get the following error: 但是,尝试加载MainWindow笔尖时,程序崩溃了,并且出现以下错误:

2010-05-05 20:24:37.211 Ride[6032:207] *** -[MyRidesViewController cancelAddingNewRide]: unrecognized selector sent to instance 0x501e450
2010-05-05 20:24:37.213 Ride[6032:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[MyRidesViewController cancelAddingNewRide]: unrecognized selector sent to instance 0x501e450'

So, I am a little lost as to why it would work one way, but not the other. 因此,我对它为什么会以一种方式而不是另一种方式起作用感到有些困惑。

First, I wanted to address part of the error: Think of your views as a stack. 首先,我想解决部分错误:将您的视图视为堆栈。 When you "push" a modal controller, you are adding that view to a stack. 当您“推”模态控制器时,您正在将该视图添加到堆栈中。 The old view is still there underneath. 旧的视图仍然在下面。 So you need to "pop" off the modal view to return to the old view. 因此,您需要“弹出”模态视图以返回到旧视图。 If you push a new view on, you now have 3 views on the stack which are all taking up memory, where you really only need one. 如果按一个新视图,则现在堆栈中有3个视图,它们全部占用内存,实际上只需要一个。

So, inside cancelAddingNewRide just try: 因此,在cancelAddingNewRide内部,只需尝试:

[super dismissModalViewControllerAnimated:true];

You may have other issues that are causing the crash, but this should generally get things working. 您可能还有其他导致崩溃的问题,但这通常可以使事情正常进行。

Typically when I have used presentModalViewController the presented viewController tells the calling viewController to dismiss it using dismissModalViewControllerAnimated:YES; 通常,当我使用presentModalViewController时,所呈现的viewController告诉调用viewController的方法是使用dismissModalViewControllerAnimated:YES;将其关闭。

So in other words in the cacncelAddingNewRide you simply call the class that hass showAddnewRideView in it and have it pass itself to the method. 因此,换句话说,在cacncelAddingNewRide中,您只需调用其中具有showAddnewRideView的类,并将其自身传递给该方法。

It's hard to explain but I'll show you an example: 很难解释,但我将向您展示一个示例:

cancelAddingNewRide class: cancelAddingNewRide类:

- (IBACtion)home:(id)sender {
    if (self.delegate respondsToSelctor:@selector(dismiss:)]) {
        [self.delegate dismiss:self];
    }
}

and then in the showAddNewRideView class 然后在showAddNewRideView类中

-(void) dismiss:(cancelAddingNewRide_class *) controller {
     [self dismissModalViewControllerAnimated:Yes];
}

Hope that makes sense and soz for typos 希望这对错别字有意义

Edit: oh and make the controller's delegate self 编辑:哦,让控制器的代表自我

controller.delegate = self;

Actually thinking about it more there is a bit more to this. 实际上,要考虑的更多。 You have to define the called viewController as a Delegate. 您必须将调用的viewController定义为Delegate。 Have a look at Stanford universities iPhone lectures, lecture 11 deals with this and is available from iTunesU . 看看斯坦福大学的iPhone讲座,第11讲涉及此内容,可以从iTunesU上获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM