2015-06-27 39 views
0

我已經知道如何在UIViewController之間傳遞值或變量。事實上,我在2 + 2 UIViewController之間有2次轉換,但是我有一個沒有這樣做的轉換。不理解如何在ViewControllers之間傳遞值

我試圖複製一切,但這個似乎並沒有工作。

這是我如何處理點擊傳遞到另一個UIViewController

-(void)launchProfile:(int) option { 
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Profile" bundle:nil]; 
    ProfileControllerViewController *vc = [sb instantiateViewControllerWithIdentifier:@"ProfileControllerViewController"]; 

    [vc passValue:_user]; 
    [vc passOption:option]; 
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self presentViewController:vc animated:YES completion:NULL]; 
} 

這是我ProfileControllerViewController.h

@interface ProfileControllerViewController : UIViewController 

-(void)passValue:(NSDictionary*)user; 
-(void)passOption:(int)option; 

@property (nonatomic , strong) NSDictionary* user; 

// ... 

@end 

This is my ProfileControllerViewController.m`(在這裏我實現該功能的一部分

@implementation ProfileControllerViewController 

int optionSelected = -1; 
const int optionChanges = 0; 
const int optionComments = 1; 

-(void)passOption:(int) option { 
    optionSelected = option; 
} 

- (void) passValue:(NSDictionary *)user_ { 
    _user = user_; 
} 

// ... 

@end 

何時它執行所passValuepassOption誤差爲:

2015年6月27日12:31:51.616 Ch4nge.me [41958:1907763] viewDidAppear 2015年6月27日12:31:56.041 Ch4nge.me [41958: 1907763] Interface Builder文件中的未知類_TtC31ProfileControllerViewController31ProfileControllerViewController。 2015-06-27 12:31:57.102 Ch4nge.me [41958:1907763] - [UIViewController passUser:]:無法識別的選擇器發送到實例0x7fb505412c00 2015-06-27 12:31:57.107 Ch4nge.me [41958:1907763 ***終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: ' - [UIViewController中passUser:]:無法識別的選擇發送到實例0x7fb505412c00'

可能是錯誤???

非常感謝您提前。

+1

從日誌中,您似乎忘記在故事板中設置類。請檢查它 – Leo

+0

也許考慮在'ProfileControllerViewController'中定義屬性,然後在launchProfile:中設置它們。它基本上是你現在正在做的同樣的事情,但一點點清潔。 – JaredH

回答

1

-[UIViewController passUser:]: unrecognized selector sent to instance表示您的vc變量不是ProfileControllerViewController類型。

可能您沒有在XIB或Storyboard中設置類,這就是爲什麼instantiateViewControllerWithIdentifier未返回預期類型;而是返回類型爲UIViewController的默認實例。

+0

因爲我之前有很多錯誤,所以我將Class設置爲'ProfileControllerViewController',但將** Module **設置爲ProfileControllerViewController(兩者)...這就是問題所在......我非常確定這個課程已經確定,這就是爲什麼我不重新看一看。非常感謝(接受答案,當它使我能夠做到這一點時:D) –

1

您可以使用:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 

{ 

    if([segue.identifier isEqualToString:@"segueIdentifer"]) //use whatever identifer you have set 

    { 

     ProfileControllerViewController *vc = (ProfileControllerViewController *)segue.destinationViewController; 

     vc.user = dict; //NSDictionary value 

     vc.option = 1; //Use whatever integer you want to pass 

    } 

} 

並在您的ProfileControllerViewController.h定義@property (nonatomic, strong) NSDictionary* user;@property (nonatomic, assign) int option;

相關問題