将值从选定行传递到视图控制器

时间:2013-06-18 13:37:23

标签: ios objective-c uitableview

我尝试将所选行的值传递给视图控制器。日志在新视图控制器中返回null值。我的错误是什么?

 2013-06-18 17:28:59.394 App[1790:c07] ID is Here: 35023

 2013-06-18 17:28:59.397 App[1790:c07] Value in Next view  (null)

2013-06-18 17:28:59.398 App[1790:c07] Label is Here <UILabel: 0x9139810; 
frame = (14 140; 255 16); clipsToBounds = YES; opaque = NO; 
autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x915ab10>>

TableViewController代码:

NSString *localStringValue;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSUInteger row = [indexPath row];
    NSString *value = [myArray objectAtIndex:row];

    DetailViewController *second=[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] ;

    second.PassID=value;

    [self presentModalViewController:second animated:YES];

    localStringValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    NSLog(@"ID is Here: %@", value);
}

下一页查看控制器代码:

·H

@interface DetailViewController : UIViewController
{
    NSString *PassID;
}
@property (nonatomic, retain) NSString *PassID;
@property (unsafe_unretained, nonatomic) IBOutlet UILabel *label;

.m代码

- (void)viewDidLoad
{

   label.text=PassID;
    NSLog(@"Label is Here %@",label);

    [super viewDidLoad];
}

3 个答案:

答案 0 :(得分:1)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSUInteger row = [indexPath row];
    NSString *value = [myArray objectAtIndex:row];

    DetailViewController *second=[[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil] ;

/// second.PassID=value; ///Remove the line here


    [self presentModalViewController:second animated:YES];

    second.PassID=value; ///assign the value after presenting


    localStringValue = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
    NSLog(@"ID is Here: %@", value);
}

答案 1 :(得分:0)

尝试NSLog(@"Label is Here %@", self.label);,看看是否有效。

.h文件中的NSString *label; ivar是不必要的,因为您还要声明具有相同名称的媒体资源。

答案 2 :(得分:0)

在DetailViewController.h中进行更改

@interface DetailViewController : UIViewController 
@property (nonatomic, retain) NSString *label;
在DetailViewController.m中

-(void)setLabelText:(NSString *)label{
    if(_label!=label)
        _label = label;
}

并使用功能

  

[second setLabelText:value];

并使用

打印
  

NSLog(@“Label is Here%@”,self.label);