2015-04-02 44 views
-2

這是我的代碼。 「ViewController.m」數組參數在像「didSelectRowAtIndexPath」和「prepareForSegue」這樣的函數中沒有被調用

-(void)viewDidLoad { 
    [super viewDidLoad]; 
    Fruit *item1 = [Fruit new]; 
    item1.name = @"Apple"; 
    item1.price = @"1$ for one"; 
    item1.image = @"apple.jpeg"; 
    item1.location = [NSArray arrayWithObjects:@"Japan", @"Apple is good for health.", nil]; 
    Fruit *item2 = [Fruit new]; 
    item2.name = @"Banana"; 
    item2.price = @"80 cents for one"; 
    item2.image = @"banana.jpeg"; 
    item2.location = [NSArray arrayWithObjects:@"Taiwan",@"Banana is good after exercise.", nil] 
    fruit = [NSMutableArray arrayWithObjects:item1,item2, nil]; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    Fruit *newRow = [fruit objectAtIndex:indexPath.row]; 
    NSString * selectedFruit = newRow.name; 
    UIAlertView * messageAlert = [[UIAlertView alloc]initWithTitle:@"Row Selected"message:selectedFruit delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"hi", nil]; 
    [messageAlert show]; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"showFruitDetail"]) 
    { 
     NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow]; 
     FruitDetailViewController * destViewController = segue.destinationViewController; 
     Fruit *newRow = [fruit objectAtIndex:indexPath.row];     
     destViewController.detailFruit = newRow; 
    } 
} 

我在兩個函數中都放入了斷點,並檢查參數「location」。他們都顯示「無」。

參數「位置」也是一個數組。如何在函數之間傳遞數組?

+1

您可以複製 – Anupam 2015-04-02 06:57:04

+1

歡迎粘貼水果類屬性的社會,你應該看看:https://開頭developer.apple.com/library/ios/referencelibrary/GettingStarted/RoadMapiOS/FirstTutorial.html – LoVo 2015-04-02 06:57:19

+0

什麼樣的參考您的財產位置使用弱或強?在我的,但它應該是強大的,以避免收回 – 2015-04-02 07:11:41

回答

0

「location」屬性應該使用「strong」引用來避免ARC自動釋放。

@property(nonatomic, strong) NSArray * location; 

入住#1以下鏈接瞭解強VS弱引用Differences between strong and weak in objective-c

+0

非常感謝。 :) – 2015-04-02 08:27:18

+0

如果您滿意我的回覆,請將其標爲您的問題答案。 – 2015-04-02 08:49:45

+0

哦,當然。我忘了。 :) 綠色複選標記正確嗎? – 2015-04-02 09:21:34

相關問題