2013-03-07 64 views
0

我有一個NSString在.h文件中聲明。然後我在ViewDidload內分配一個值。在我的didSelectRowatIndex事件中想要將該值賦給先前ViewController中的另一個字符串。無法訪問viewdidload之外的NSString

.H

`

NSString *strWifiId; 

NSArray *arrstrWifiId;` 

`
- (無效)viewDidLoad中 {

 arrstrWifiId=[arrWorkflow valueForKey:@"WFID"]; 

     strWifiId=[NSString stringWithFormat:@"%@",[arrstrWifiId objectAtIndex:0] ]; 
} 

`

`

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

NSArray *array = [self.navigationController viewControllers]; 

NSLog(@"navi array count ----- %d",[array count]); 


UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath]; 




for (UIViewController* viewController in self.navigationController.viewControllers) { 


    if ([viewController isKindOfClass:[SendTimeChangeRequestViewController class]]) { 


     SendTimeChangeRequestViewController *groupViewController = (SendTimeChangeRequestViewController*)viewController; 


     [groupViewController.btnwrkflwFirstApprove setTitle:selectedCell.textLabel.text forState:UIControlStateNormal]; 


     groupViewController.pWorkFlowID=strWifiId; 

     NSLog(@"WFID---- %@",groupViewController.pWorkFlowID); 
     //NSLog(@"CELL TAG----- %@",groupViewController.pAuthEmployeeCode); 


     [self.navigationController popToViewController:groupViewController animated:YES]; 
    } 
} 

`

這裏NSLog打印一些垃圾值。爲什麼是這個?我該如何解決這個問題?

感謝

回答

0

您擁有該對象從initWithFormat您是負責釋放返回,但你沒有自己的對象從stringWithFormat它返回一個字符串,自動釋放,因此不需要將其釋放回(如果你確實想擁有它,你必須保留它)。

所以解決您的問題,嘗試分配你的價值就是這樣,

- (void)viewDidLoad { 

     arrstrWifiId=[arrWorkflow valueForKey:@"WFID"]; 

     strWifiId=[[NSString alloc] initWithFormat:@"%@",[arrstrWifiId objectAtIndex:0] ]; 
}