无法访问viewdidload之外的NSString

时间:2013-03-07 10:58:55

标签: ios6 nsstring nsarray

我在.h文件中声明了NSString。然后我在ViewDidload内分配一个值。在我的didSelectRowatIndex事件中,想要将该值分配给先前Viewcontroller中的另一个字符串。

·H

`

 NSString *strWifiId;

 NSArray *arrstrWifiId;`

`
       - (void)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打印一些垃圾值。为什么我能解决这个问题?

由于

1 个答案:

答案 0 :(得分:0)

您拥有从您负责发布的initWithFormat返回的对象,但您不拥有从stringWithFormat返回的返回自动释放字符串的对象,因此不需要释放它(如果你想拥有它,你必须保留它。)

因此,要解决您的问题,请尝试分配您的值,

- (void)viewDidLoad {

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

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