我想從一個VC傳遞一個int到另一個。我正在這樣做。傳遞一個Int從一個ViewController到另一個使用segue
在我的VC1中,當我調用prepareForSegue方法時,我在做一個segue showDetail
。它看起來像這樣。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = (NSIndexPath *)sender;
News *news = [arrNews objectAtIndex:indexPath.row];
NSLog(@"news id is %@",news.new_id);
if ([segue.identifier isEqualToString:@"showDetail"]) {
[segue.destinationViewController setNewsId:(int)news.new_id];
}
}
在日誌中我得到了正確的數字。現在在我的VC2中,我已經聲明瞭這樣一個int。
@property (nonatomic) int newsId;
而我的setMethod看起來像這樣。
-(void)setNewsId:(int)newsId{
NSLog(@"news id is %d",newsId);
self.newsId = newsId;
}
當我執行時,我進入一個循環與所有大數字。有人可以幫我解決這個問題嗎?
news.new_id的類型是什麼?爲什麼你需要在-setNewsId:call中將其轉換爲int? – Gereon 2013-03-19 08:03:23
看到我的回答.... – 2013-03-19 08:13:33