不能推UIViewController

时间:2014-08-18 06:42:44

标签: ios objective-c uiviewcontroller

我在推送视图控制器时刷新了UI的问题。这就是我正在做的事情......

因此,当我的应用收到推送通知时,会从我的didReceiveRemoteNotification调用AppDelegate方法,然后执行以下操作:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

                ReceiptViewController *rv;

                if(screenHeight == 480)
                {
                    rv = [storyboard instantiateViewControllerWithIdentifier:@"receiptView2"];
                }else{
                    rv = [storyboard instantiateViewControllerWithIdentifier:@"receiptView"];
                }
                //ReceiptViewController *rv = [storyboard instantiateViewControllerWithIdentifier:@"receiptView"];

                rv.orderID = userInfo[@"orderID"];
                rv.driverID = userInfo[@"driverID"];
                rv.cost = [userInfo[@"cost"] intValue];

                [(UINavigationController *)self.window.rootViewController pushViewController:rv animated:YES];

所以这里^,我基本上将它重定向到ViewController,具体取决于屏幕尺寸(3.5英寸或4英寸)。这很好。

但是,在我的ReceiptViewController中,我有一个按钮,其中包含以下操作:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PickupViewController *pv;

    if(screenHeight == 480)
    {
        NSLog(@"---------------------------I'm @ 480 Height!!!---------------------------");

        pv = [storyboard instantiateViewControllerWithIdentifier:@"pickupView2"];
    }else{

        NSLog(@"---------------------------I'm @ 568 Height!!!---------------------------");

        pv = [storyboard instantiateViewControllerWithIdentifier:@"pickupView"];
    }



    NSLog(@"-------------------Going to PickupViewController!!!!-------------------");

    [self.navigationController pushViewController:pv animated:YES];

现在这里是奇怪的部分。当点击按钮并执行上面的代码块时,init中的PickupViewController方法就会运行(我知道这是因为我看到我的NSLog出现了它),但我仍然看到ReceiptViewController。虽然它仍然在ReceiptViewController,但它仍然具有响应性。不知道我在这里做错了什么。任何帮助将不胜感激!

-----------的修改 ---------------------

我还应该提一下,只有在didReceiveRemoteNotification方法中推送到ReceiptViewController时(当我收到推送通知时),这只发生在AppDelegate方法中。如果我在AppDelegate的applicationWillEnterForeground中执行完全相同的操作,并将完全相同的参数传递给ReceiptViewController,它就可以正常工作。

-----------的更新 ---------------------

点击按钮时,我运行了以下内容:

NSUInteger countViews = [self.navigationController.viewControllers count];
    NSString *CurrentSelectedViewController = NSStringFromClass([[self.navigationController visibleViewController] class]);

    NSLog(@"---------------------------Current ViewController => %@, Total ViewControllers on NavController => %tu", CurrentSelectedViewController, countViews);

我正在找回ViewController&的正确名称。它说堆栈数是7.不确定这里发生了什么......所以我引用了正确的导航控制器&它不是空的。如果我这样做:

[self.navigationController popToRootViewControllerAnimated:YES];

我回来了EXC_CRASH(SIGABRT)

Thread 0 Crashed:
0   libobjc.A.dylib                 0x0000000196c181d0 objc_msgSend + 16
1   UIKit                           0x000000018d6fd040 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 20
2   UIKit                           0x000000018d6e651c -[UIControl _sendActionsForEvents:withEvent:] + 372
3   UIKit                           0x000000018d6fca40 -[UIControl touchesEnded:withEvent:] + 580
4   UIKit                           0x000000018d6fc6d4 -[UIWindow _sendTouchesForEvent:] + 688
5   UIKit                           0x000000018d6f736c -[UIWindow sendEvent:] + 1168
6   UIKit                           0x000000018d6c8b4c -[UIApplication sendEvent:] + 252
7   UIKit                           0x000000018d6c6c3c _UIApplicationHandleEventQueue + 8496
8   CoreFoundation                  0x000000018a6bf7f0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 20
9   CoreFoundation                  0x000000018a6beb4c __CFRunLoopDoSources0 + 252
10  CoreFoundation                  0x000000018a6bcde4 __CFRunLoopRun + 628
11  CoreFoundation                  0x000000018a5fddcc CFRunLoopRunSpecific + 448
12  GraphicsServices                0x00000001902e5c08 GSEventRunModal + 164
13  UIKit                           0x000000018d72efc0 UIApplicationMain + 1152
14  AriaBlackCar                    0x000000010004baf0 main (main.m:16)
15  libdyld.dylib                   0x00000001971fba9c start + 0

4 个答案:

答案 0 :(得分:0)

您可以使用segue替代此

[self.navigationController pushViewController:pv animated:YES];

因为有些时候这条线不起作用。这是一个编译器错误。并清理您的应用程序的生成文件夹。使用命令+ shift + alter + k。 并在设备中测试它。 这解决了我的问题。

答案 1 :(得分:0)

UIStoryboard *storyboard =  [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];

//创建故事板的对象 UIViewController * ivc = [storyboard instantiateViewControllerWithIdentifier:@“视图控制器的名称”];

//创建一个视图控制器,然后推送它                                                                         [self.navigationcontroller pushViewController:ivc animated:YES];

答案 2 :(得分:0)

正在从此行调用init中的PickupViewController

[storyboard instantiateViewControllerWithIdentifier:@"pickupView2"];

这就是你从NSLog获得PickupViewController的原因。根据我的理解,导航部分失败了。尝试像在第一个块中那样调用导航控制器,例如

[(UINavigationController *)self.window.rootViewController pushViewController:rv animated:YES];

答案 3 :(得分:0)

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@“Main_iPhone”bundle:nil]; //创建故事板对象

 

UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"name of your view controller"];

//创建一个视图控制器,然后推送它                                                                         

    [self.navigationcontroller pushViewController:ivc animated:YES];