2013-07-16 116 views
1

我是iOS开发新手,目前一直在创建后退按钮。我已经将popViewControllerAnimated方法放在敲击按钮上,我也在调试模式下测试它,并且后​​退按钮方法正在调用,但popViewControllerAnimated不起作用。后退按钮没有响应?

这是代码。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.height-100), 30, 80, 20)]; 
    [backButton setTitle:@"Back" forState:UIControlStateNormal]; 
    [backButton addTarget:self action:@selector(BackButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:backButton]; 

    //some piece of code... 

}//end of viewDidLoad method 


-(void)BackButtonClicked: (id)sender{ 
    [self.navigationController popViewControllerAnimated:YES]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"testing" message:@"some message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"NO", nil]; 
    [alert show]; 

} 

在点击返回按钮时显示此警报,但屏幕不回退。

有关此问题的任何帮助将非常可观。

我在我以前的viewController编写这些代码叫这个的viewController

PlaceTranslatedDetailsViewController *secondViewController = 
[self.storyboard instantiateViewControllerWithIdentifier:@"placeTranslatedDetail"]; 
[self presentViewController:secondViewController animated:YES completion:NULL]; 

然而,这种的viewController和以前的viewController设计编程

+3

你的视图控制器推送什么?它是如何加载和呈现的? – pdrcabrod

+0

编写本代码的视图控制器 – iPatel

+0

请现在审查,我对我的问题的内容做了一些更改 –

回答

1

使用此代码:

[self dismissViewControllerAnimated:YES completion:NULL] 
+0

非常感谢Kirti,它的工作原理! :) –

+0

当前屏幕正在向后滑动按下按钮,是否可以将其滑动到右侧攻丝? –

+0

您还在右侧添加了uibarbuttonitem –

2

如果您viewcontroller不是一个uinavigationcontroller比方法的rootviewcontrollerpopViewControllerAnimated将不起作用。所以你需要有一个uinavigationcontroller,以推/ viewcontroller

所以在你的情况下,你必须使用[self dismissViewControllerAnimated:YES completion:nil];而不是popViewController,因为你提出了一个模态的视图控制器。

+0

这应该是被接受的答案。 +1来解释问题。 – Amar

+1

@Amar谢谢,没关系,我只希望他读到我在这里解释的。 – soryngod

2

您还没有出示您的视图控制器作为导航堆栈的一部分,所以使用popViewControlleranimated:是行不通的。

您需要使用

[self dismissViewControllerAnimated:YES completion:NULL]; 

代替。

1

一推(经由pushViewController或情节串连图板推赛格瑞)将需要popViewController

呼叫甲呈现视图(经由presentVewController或情节串连图板模态赛格瑞)需要一个dismissViewController呼叫。