2011-11-30 21 views
0

出租以考慮代碼如何變一類的方法,在目標C傳給其他類

one.h 
#import "historyViewController.h" 

int isItfsa; 


one.m 
    -(IBAction)homeHistory:(id)sender{ 
      isItfsa = 0; 
      historyViewController *hisVController = [[historyViewController alloc]initWithNibName:@"historyViewController" bundle:nil]; 
      [self presentModalViewController:hisVController animated:YES]; 
      [hisVController release]; 
     } 

但是,當我收到它不打印// 0

但兩個二等。 m

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    NSLog(@"isItfsa=%@",isItfsa); //isItfsa=null 
} 

它是打印null,但爲什麼?以及如何通過方法將值從一個類傳遞到另一個類。

+1

澄清您的問題。 「通過方法將價值從一類傳遞給另一類」是什麼意思?簡單的回答將是「創建一個方法參數」, – Sulthan

回答

1

空值與零值相同。它寫入「null」是因爲你不告訴它寫一個整數,而是告訴它寫一個對象。

您的代碼:

NSLog(@"isItfsa=%@",isItfsa)(寫isItfsa爲對象)

它應該是:

NSLog(@"isItfsa=%i",isItfsa)(寫isItfsa爲整數)

2

isItfsa是由你的代碼作爲一個int定義,這不是一個對象。您的NSLog格式化爲使用%@打印對象。如果int值爲零,那麼試圖將其作爲對象打印將產生(null)。

您的打印報告應爲NSLog(@"isItfsa=%d",isItfsa);

+0

爲什麼%d是一個整數?這只是另一個錯誤。 – Sulthan

+0

%我也工作。只需遵循以下文檔:http://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html – Jim

+1

啊,你是對的。我的印象是,%d意味着雙倍。有時重新訪問文檔:)是很好的。 – Sulthan

相關問題