問題描述:建立了一個列表應用,只要按按鈕就可以添加字串到mutable數組中。不過My Code運行之後,點擊按鈕只有最後的數組添加成功了。[plain] - (IBAction)notebutton:(UIButton *)sender { NSMutableArray *mystr = [[NSMutableArray alloc] init]; NSString *name = _noteField.text; [mystr addObject:name]; [self.tableView reloadData]; } 解決方案:這是因為,你每次點擊這個按鈕的時候都會重新建立NSMutableArray 對象[plain] - (IBAction)notebutton:(UIButton *)sender { NSMutableArray *mystr = [[NSMutableArray alloc] init]; 如何解決?你只需要將*mystr聲明放到標頭檔中,作為屬性或私人變數來定義。如[plain] @interface yourClass:NSObject { NSMutableArray *mystr; } @end 在.m的init方法中來初始化這個NSMutableArray[plain] @implementation yourClass -(id)init { if (self=[super init]) { mystr=[[[NSMutableArray alloc] initWithCapacity:0] autorelease]; } } @end 做完這兩步,你就可以直接在 - (IBAction)notebutton:(UIButton *)sender { NSString *name = _noteField.text; [mystr addObject:name]; [self.tableView reloadData]; }