2013-05-29 98 views
1

我推送一個字符串從一個UIViewController到另一個到目前爲止,我成功做到了這一點,但是,當我將推送的字符串添加到NSMutableArray和NSLog數組總數時,它總是返回0這裏是我的代碼:我正在使用故事板,並且是否需要將第二個視圖引用到視圖中進行加載?推送數據從一個UIViewController到另一個

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    [_TableView setDelegate:self]; 
    //[_TableView setDataSource:self]; 
    _numberOfRows = [[NSMutableArray alloc]init]; 
} 

-(void)pushName:(NSString*)contactName 
{ 
    //NSLog(@"Pushed name is: %@ ", contactName); 
    _contactName = contactName; 
    NSLog(@"Pushed name is: %@ ", _contactName);//<-- Returns the pushed string fine 

    [_numberOfRows addObject:contactName]; 
    //[self.TableView reloadData]; 
    NSLog(@"Number of names: %d ", [_numberOfRows count]);//<--Always returns a 0 

} 

不太確定爲什麼這不起作用。如果需要更多的代碼來澄清這個問題,那麼請不要問。

+0

看看這裏http://stackoverflow.com/a/16142163/1328096 – jamil

+0

@Undo不,這不是一個重複的問題我沒有通過一個NSMutableArray im傳遞一個單一的字符串我只想填充一個數組第二個UIViewController他們是完全不同的問題bro – Led

+0

@one同樣的原則。任何傳遞數組的東西都可以傳遞一個字符串。只需查看答案並用'NSString'替換'NSMutableArray'即可。 – Undo

回答

1

當您調用pushName時,您的數組似乎尚未實例化。

您能否向我們提供您稱之爲何處的環境?

編輯:只是闡述。你的問題不是在兩個控制器之間傳遞數據。你的問題似乎是你的數組沒有響應addObject和計數消息。

嘗試在你的數組上使用懶惰實例,它應該工作。

編輯2:代碼片段: 嘗試添加方法:

- (NSMutableArray *)numberOfRows: 
{ 
    if(_numberOfRows == nil) _numberOfRows = [[NSMutableArray alloc] init] 
    return _numberOfRows 
} 

你在哪裏使用_numberOfRows嘗試使用self.numberOfRows,即

[self.numberOfRows addObject:contactName]; 
//[self.TableView reloadData]; 
NSLog(@"Number of names: %d ", [self.numberOfRows count]);//<--Should return 1 or more now. 

編輯3:哦,如果你做,你可以刪除viewDidLoad上的實例。但你知道這一點。

編輯4:修復代碼片段。對不起,我編輯這麼多。只是試圖確保你得到正確的答案。

+0

非常感謝我正在尋找 – Led

相關問題