推送时数据未传递给视图控制器

时间:2012-01-10 03:27:37

标签: ios xcode uiviewcontroller uinavigationcontroller properties

我有一个tableviewcontroller,它有数据填充。当用户点击一个单元格时,我正试图获取一个新的详细信息视图。我可以加载视图,但不更新更详细显示数据的标签。这是代码:

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

SearchDetailViewController *controller = [self.storyboard  instantiateViewControllerWithIdentifier:@"searchDetailVC"];
Events *event = [events objectAtIndex:indexPath.row];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [dateFormatter stringFromDate:event.eventDate];

controller.title=event.eventName; //ONLY ONE THAT WORKS!


controller.eventName.text = event.eventName;
controller.eventAddress.text = event.eventAddress;
controller.eventDate.text = dateString;
controller.eventTime.text = event.eventTime;
controller.ratingView.image = [self imageForRating:event.eventRating];
controller.eventDistance.text = @"NOT YET IMPLEMENTED";

[self.navigationController pushViewController:controller animated:YES];
}

设置的唯一属性是NavigationController导航栏的标题。我做了一件大错的事吗?

1 个答案:

答案 0 :(得分:1)

您无法传递此类数据。 ViewController.domainField当时为nil,因为视图尚未加载,标签/ TextField是在视图加载时(通过nib)创建的,而不是在创建视图控制器对象时创建的。

此代码可以帮助您

SearchDetailViewController.h
    #import <UIKit/UIKit.h>
    @interface SearchDetailViewController : UIViewController 
    {

        NSString*  eventNameString ;
        NSString* eventAddressString;
        ...........
        .............

    }
    @property (nonatomic,retain) NSString*  eventNameString ;
    @property (nonatomic,retain) NSString*  eventAddressString;
    ...............
    ...............
    @end


SearchDetailViewController.m
- (void)viewDidLoad
{
     [super viewDidLoad];
     eventName.text=eventNameString;
     eventAddress.text=eventAddressString;
     ..........................
     ..........................
    eventNameString=[[NSString alloc]initWithString:@""];
}


TableView.m

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

[tableView deselectRowAtIndexPath:indexPath animated:YES];

SearchDetailViewController *controller = [self.storyboard  instantiateViewControllerWithIdentifier:@"searchDetailVC"];
Events *event = [events objectAtIndex:indexPath.row];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *dateString = [dateFormatter stringFromDate:event.eventDate];

controller.title=event.eventName; //ONLY ONE THAT WORKS!


controller.eventNameString=[[NSString alloc]initWithString:event.eventName];
controller.eventAddressString=[[NSString alloc]initWithString:eventAddress];
...............................
...............................
............

[self.navigationController pushViewController:controller animated:YES];
}

同样,您必须将所有数据从一个视图传递到另一个视图 - 您无法直接分配给控制器。