在viewcontrollers之间传输变量

时间:2013-04-06 16:07:27

标签: objective-c variables viewcontroller

我的iOS App Project出了问题。我想将我的SearchViewController的值传递给我的ResultGeneralInfoViewController。没有错误或问题,但它不起作用。

这是我的SearchViewController.m中的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *item = [searchResults objectAtIndex:[indexPath row]];


    ResultGeneralInfosViewController *detailViewController = [ResultGeneralInfosViewController alloc];
    detailViewController.companyName = [item objectForKey:@"companyName"];
    NSLog([item objectForKey:@"companyName"]);
}

ResultGeneralInfoViewController.h

@interface ResultGeneralInfosViewController : UIViewController {

    NSString *companyName;
}

@property NSString *companyName;

@property (weak, nonatomic) IBOutlet UILabel *companyNameLabel;

和ResultGeneralInfoViewController.m

@implementation ResultGeneralInfosViewController

@synthesize companyName;

//Outlets
@synthesize companyNameLabel;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    companyNameLabel.text = self.companyName;
}

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

您尚未初始化ResultGeneralInfosViewController,仅分配了它。调用相应的init方法以便首先对其进行初始化。例如:

ResultGeneralInfosViewController *detailViewController =
    [[ResultGeneralInfosViewController alloc] initWithNibName:@"NibName" bundle:nil];

确保在初始化对象后设置属性。

答案 1 :(得分:1)

didSelectRowAtIndexPath中,您创建了ResultGeneralInfosViewController的实例,但您没有对其执行任何操作。如果您使用导航控制器,则应添加

[self.navigationController pushViewController:detailViewController animated:YES];