将字符串值传递给另一个控制器

时间:2011-08-08 10:44:07

标签: iphone objective-c

我正在将string传递给另一个控制器

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

CategoriesList *dvController = [[CategoriesList alloc] initWithNibName:@"CategoriesList" bundle:nil];

NSString *category=aBook.name;
dvController.category=category;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];

}

当我在dvController中获得价值时,它没有显示我将其设置为按钮标题

 NSString *title=category;
 [titleButton setTitle:title forState:UIControlStateNormal];

1 个答案:

答案 0 :(得分:2)

最简单的方法是在CategoriesList中创建自定义init:

CategoriesList.h

@interface DetailViewController : UIViewController
{
    NSString *category;
}

@property (nonatomic, retain) NSString *category;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil category:(NSString *)categoryName;

CategoriesList.m

@synthesize category;

...

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

...

- (void)dealloc
{
    [category release];
    [super dealloc];
}