从 Restkit 成功的上下文中以编程方式导航视图控制器

Posted

技术标签:

【中文标题】从 Restkit 成功的上下文中以编程方式导航视图控制器【英文标题】:Programmatically navigate view controllers from the context of Restkit success 【发布时间】:2014-05-25 23:43:39 【问题描述】:

我正试图让我的代码枯竭,所以我将 restkit 方法从 ViewController 中提取到它们自己的模型中。因此,成功后,我想:

    将当前视图控制器自身传递给函数 实例化一个新的视图控制器 在视图控制器之间导航 - 从 API 对象中成功函数的上下文中

我从我的视图控制器调用此代码:

@interface snapViewController ()
@property (strong, nonatomic) snapApi *api;
@property (weak, nonatomic) IBOutlet UITextField *userEmail;
@property (weak, nonatomic) IBOutlet UITextField *userPassword;  
@end

@implementation snapViewController
- (IBAction)firePostCreateAtApi:(id)sender 
    [self.api createNewUser:self.userEmail.text userPassword:self.userPassword.text viewController:self];


这里是代码(失败代码)查看成功:

// Hit the CreateNewUser Endpoint

- (void)createNewUser:(NSString *)userName userPassword:(NSString *)password viewController:viewController

    NSString *token = SNAPTOKEN;
    NSDictionary *profileQueryParams = @
                              @"password":password,
                              @"email":userName,
                              @"headline":@"headline";

    NSDictionary *queryParams = @@"accesstoken" : token,
                                  @"profile" : profileQueryParams;

    [[RKObjectManager sharedManager] postObject: nil
                                           path: @"/new"
                                     parameters:queryParams
                                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
                                            NSLog(@"viewcontroller: %@", viewController);
                                            snapHomeViewController *snap = [[snapHomeViewController alloc] initWithNibName:@"viewControllerB" bundle:[NSBundle mainBundle]];
                                            snap.profile = mappingResult.array;
                                            NSLog(@"snap: %@", snap);
                                            [viewController pushViewController:snap animated:YES];


                                        
                                        failure:^(RKObjectRequestOperation *operation, NSError *error) 
                                            NSLog(@"What do you mean by 'there is no coffee?': %@", error);
                                        ];

例外:

2014-05-25 16:34:00.403 SnapDate[35639:60b] viewcontroller: <snapViewController: 0x10a20ba20>
2014-05-25 16:34:00.403 SnapDate[35639:60b] snap: <snapHomeViewController: 0x10a301c60>
2014-05-25 16:34:00.403 SnapDate[35639:60b] -[snapViewController pushViewController:animated:]: unrecognized selector sent to instance 0x10a20ba20
2014-05-25 16:34:00.405 SnapDate[35639:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[snapViewController pushViewController:animated:]: unrecognized selector sent to instance 0x10a20ba20'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102371495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001020d099e objc_exception_throw + 43
    2   CoreFoundation                      0x000000010240265d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000102362d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000102362938 _CF_forwarding_prep_0 + 120
    5   SnapDate                            0x0000000100003145 __53-[snapApi createNewUser:userPassword:viewController:]_block_invoke + 357
    6   SnapDate                            0x00000001000b69cb __66-[RKObjectRequestOperation setCompletionBlockWithSuccess:failure:]_block_invoke244 + 91
    7   libdispatch.dylib                   0x0000000102c6b851 _dispatch_call_block_and_release + 12
    8   libdispatch.dylib                   0x0000000102c7e72d _dispatch_client_callout + 8
    9   libdispatch.dylib                   0x0000000102c6e3fc _dispatch_main_queue_callback_4CF + 354
    10  CoreFoundation                      0x00000001023cf289 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    11  CoreFoundation                      0x000000010231c854 __CFRunLoopRun + 1764
    12  CoreFoundation                      0x000000010231bd83 CFRunLoopRunSpecific + 467
    13  GraphicsServices                    0x00000001033b2f04 GSEventRunModal + 161
    14  UIKit                               0x0000000100c7de33 UIApplicationMain + 1010
    15  SnapDate                            0x0000000100003313 main + 115
    16  libdyld.dylib                       0x0000000102ecf5fd start + 1
    17  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

如何从 Success 的上下文中以编程方式在 ViewController 之间导航?

【问题讨论】:

pushViewController:animated:UINavigationController 的方法,而不是UIViewController 好的,有没有类似的方法可以让我以编程方式在 ViewController 之间导航? 【参考方案1】:

如果您的viewControllerUINavigationController 内,那么您需要致电:

[viewController.navigationController pushViewController:snap animated:YES]; 

否则,您无法将其显示为推送,您必须通过调用以下方式以模态方式显示它

[viewController presentModalViewController:snap  animated:YES];

【讨论】:

也做错了: UIStoryboard *sboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; snapHomeViewController *vc1 = [sboard instantiateViewControllerWithIdentifier:@"snaphome"];

以上是关于从 Restkit 成功的上下文中以编程方式导航视图控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中以编程方式在片段之间导航?

在 React-Router v4 中以编程方式导航

如何向在 tabcontroller 中以编程方式创建的导航控制器中的 barbuttonitem 添加操作

在 JBoss 5.1 中以编程方式确定 JNDI 根上下文/耳朵名称

可以在加载的 Spring 上下文中以编程方式替换 Spring Bean

在 iOS 中以编程方式创建 UINavigationController