无法弹出iOS viewController。不确定,但我认为它与导航控制器有关

时间:2014-03-24 16:13:39

标签: ios objective-c uiviewcontroller uinavigationcontroller

我在尝试弹出视图时遇到了麻烦

App Delegate

@implementation MAAppDelegate
@synthesize navController;
@synthesize detailViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Init the navController for the Master Detail View of the grade cells

    UINavigationController *navController = [[UINavigationController alloc] init];


    detailViewController = [[UIViewController alloc] init]; //step6
    navController = [[UINavigationController alloc] initWithRootViewController:[[MAController alloc] init]]; //step7


    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = navController; //step8
    [self.window makeKeyAndVisible];


    // Set MAController as rootViewController
    //self.window.rootViewController = [[MAController alloc] init];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];




    // Use the insanely cool TSMessages to show network alerts
    [TSMessage setDefaultViewController: self.window.rootViewController];
    return YES;
}

viewController的第一部分

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    [self.navigationController setNavigationBarHidden:YES];
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStyleBordered target:self action:@selector(home:)];
    self.navigationItem.leftBarButtonItem=newBackButton;

稍后,当我更改viewController

    NSLog(@"Opened progress report");

    UIViewController *detailViewControl = [[UIViewController alloc] init];

    // Set progress report as the view controller
    [self.navigationController pushViewController:detailViewControl animated:YES];

    UIImage *background = [UIImage imageNamed:@"bg"];

    // Add static image bg
    self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
    self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:self.backgroundImageView];

    // Add blurred layer to image when tableView goes in front of it
    self.blurredImageView = [[UIImageView alloc] init];
    self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.blurredImageView.alpha = 0;
    [self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
    [self.view addSubview:self.blurredImageView];



    [self.navigationController setNavigationBarHidden:NO];

所以我不明白为什么当我这样做时,按钮上的一个选择器(我知道它会触发,因为我在我的日志中得到了Righthtere):

    -(void)home:(UIBarButtonItem *)sender {
    NSLog(@"Righthtere");
    // Set progress report as the view controller
    [self.navigationController popToViewController:self animated:YES];

    }

它不会返回初始视图控制器。

2 个答案:

答案 0 :(得分:3)

你好像混淆了popToViewController和popViewControllerAnimated。 popViewControllerAnimated从堆栈中删除当前视图,并将新堆栈置于活动视图控制器之上。 popToViewController弹出堆栈,直到列出的视图控制器位于堆栈顶部。

由于您使用self调用popToViewController,它将查看所​​请求的视图控制器已经位于堆栈顶部并且什么都不做。如果您想返回一个视图控制器,那么您的呼叫应该是。

[self.navigationController popViewControllerAnimated:YES];

答案 1 :(得分:-2)

我使用以下代码在iOS 8中弹出上一个viewcontroller。

[self presentModalViewController:viewcontroller animated:YES];