2013-04-02 51 views
0

我有一個應用程序,它有一個叫做MainViewController的第一個視圖控制器,它不像秒錶,當我點擊完成時,它顯示一個UIAlertView推送另一個視圖控制器EndViewController,它顯示秒錶運行了多久,暫停了多久。我收到以下錯誤:iOS:在兩個視圖控制器之間共享數據時遇到問題

My Point[851:c07] -[EndViewController topViewController]: unrecognized selector sent to instance 0x754db60 

My Point[851:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[EndViewController topViewController]: unrecognized selector sent to instance 0x754db60' 

任何人都可以指出我對什麼是錯?

繼承人我的代碼:

MainViewController.h:

#import "EndViewController.h" 
@interface MainViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, UIAlertViewDelegate> 
@property (strong, nonatomic) IBOutlet UILabel *out; 
@property (strong, nonatomic) IBOutlet UILabel *in; 
@end 

MainViewController.m:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"endtask"]){ 
     UINavigationController *navController = (UINavigationController *)segue.destinationViewController; 
     EndViewController *controller = (EndViewController *)navController.topViewController; 
     controller.timeIn.text=_in.text; 

    } 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    _alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?" 
                message:nil 
                delegate:self 
              cancelButtonTitle:@"NO" 
              otherButtonTitles:@"YES", nil]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     //do nothing 
    } 
    else if (buttonIndex == 1) { 

     [self performSegueWithIdentifier:@"endtask" sender:self]; 
    }} 

- (IBAction)endButton:(UIButton *)sender { 
    [_alert show]; 
} 
- (IBAction)endButton:(UIButton *)sender { 
    [_alert show]; 
} 

EndViewController.h:

@interface EndViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *timeIn; 
@end 
+0

嘗試在prepareForSegue中記錄segue.destinationViewController,並查看給了你什麼。 – rdelmar

+0

這可能會有所幫助... http://stackoverflow.com/questions/6563597/iphone-difference-between-topviewcontroller-and-other-forms – SomeSillyName

回答

0

你調用一個方法名爲topViewController上沒有方法/屬性的對象。這是這條線的位置:

EndViewController *controller = (EndViewController *)navController.topViewController; 

我會懷疑它的,因爲你是分配給navController視圖控制器不實際上導航控制器,而是別的東西。檢查你的故事板/筆尖爲此。

另外,請嘗試記錄navController,正如一位評論者所說,並看看有什麼給你。

NSLog(@"%@", navController); 
+0

我不太瞭解這個,但我有一個視圖控制器是連接到導航控制器的根視圖控制器,然後該控制器將MainViewController推到堆棧頂部 – dietbacon

+0

@dietbacon日誌會給你什麼? – Undo

0

如果你傳遞一個字符串timeIn你應該有一個的NSString的@property在endViewController.h

@property (strong, nonatomic) NSString * timeIn ; 

然後在你endViewController.m你把timeIn到例如拉貝。

myLabel.text = timeIn; 
+0

我已經改變了,但它不是問題:( – dietbacon

+0

你是否從_in.text得到你想要的東西?你有沒有嘗試記錄它?你可能想使用標籤中的字符串而不是標籤本身。它正確。 – hugocarlmartin

相關問題