2013-03-11 30 views
1

我有兩個視圖控制器:BSViewControllerBSotherViewControllerBSViewController上有一個按鈕,可以跳到BSotherViewController。這部分工作正常,當我在模擬器上運行時,我可以看到一個VC,然後看到「其他」VC。但是我在BSotherViewController-viewDidLoad中有一個NSLog(@"other");,它沒有顯示在控制檯中。在控制檯中顯示的BSViewController-viewDidLoad中的類似NSLog(@"other");。我懷疑問題在於缺少IBAction,但我無法確定它應該到達的位置以及它應該如何在故事板中鏈接。視圖確實加載但不是viewDidLoad

BSViewController.h

#import <UIKit/UIKit.h> 

@interface BSViewController : UIViewController 

@property (nonatomic) NSInteger number; 
@end 

BSViewController.m

#import "BSViewController.h" 

@interface BSViewController() 
@end 
@implementation BSViewController 
@synthesize number; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.number = 25; 
    NSLog(@"number: %d", self.number); 
} 

@end 

BSotherViewController.h

#import <UIKit/UIKit.h> 

@interface BSotherViewController : UIViewController 
@property (nonatomic) NSInteger anumber; 
@end 

BSotherViewController.m

#import "BSotherViewController.h" 
#include "BSViewController.h" 
@interface BSotherViewController() 

@end 

@implementation BSotherViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    NSLog(@"other number:"); 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    BSViewController *view = [[BSViewController alloc] init]; 
    self.anumber = view.number; 
    NSLog(@"other number: %d", self.anumber); 
} 

storyboard

+0

嘿零,一個簡單的解決辦法是隻使用viewWillAppear中或ViewDidAppear ,不保證每次都會調用ViewDidLoad ...它很可能在BSViewController中被調用,因爲當你啓動應用程序到模擬器時,它會運行它新鮮的,因此需要加載視圖。有時使用模態段和事情,它不是必須調用viewDidLoad .... – 2013-03-11 21:27:43

回答

1

故事板中的第二個視圖控制器設置爲BSotherViewController?您可以通過將鼠標懸停在故事板中的視圖控制器圖標上進行驗證。

hovering

如果不是,然後單擊它並轉到身份控制器和輸入。

identity

+0

現在我可以解決真正的問題:爲什麼'其他數字'0而不是25?羅傑斯先生,很好的診斷。 – zerowords 2013-03-11 21:54:07

+0

這是因爲BSViewController的視圖尚未加載。 25的初始化發生在'viewDidLoad'中。您可以在記錄'anumber'之前通過記錄視圖控制器的'view'屬性來驗證它。 – 2013-03-12 15:25:38

相關問題