tableView didSelectRowAtIndexPath在iOS 7上无法正常工作.为什么?

Fra*_*ade 3 uitabbarcontroller uitableview uinavigationcontroller ios ios7

首先,我想说我只是提出这个问题,因为我想了解发生了什么.

我在Xcode5上的全新安装中打开了一个旧的Xcode项目(一个非常简单的项目).当我意识到它在iOS 7上不起作用时.为什么?不知道..

我看到了其他一些问题,没有得到任何有用的答案,所以我做了一个简单的测试.在UITableViewController所有工作正常,除了didSelectRowAtIndexPath

看看这个

RootViewController.h:

@interface RootViewController : UITableViewController

@property (strong, nonatomic) NSMutableArray *items;
@end
Run Code Online (Sandbox Code Playgroud)

RootViewController.m在viewDidLoad中我初始化数组(带有一些字符串).

实现dataSource和委托方法(是的,我将委托和dataSource设置为tableView)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_items count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [_items objectAtIndex:indexPath.row];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

问题出在这里:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:NO];

    detailViewController.detailLabel.text = [_items objectAtIndex:indexPath.row];
}
Run Code Online (Sandbox Code Playgroud)

DetailViewController 是一个简单的UIViewController:

(是的,我在nib文件上设置了IBOutlet)

@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;

@end
Run Code Online (Sandbox Code Playgroud)

问题是这在iOS7上不起作用,标签中DetailViewController没有得到更新.我尝试在之前和之后设置标签文本pushViewController.

这适用于所有以前版本的iOS.

为什么不在iOS 7上工作?

我得到这个工作的唯一方法是,就像我在其他问题上看到的那样:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];

    dispatch_async(dispatch_get_main_queue(), ^{
        detailViewController.detailLabel.text = [_items objectAtIndex:indexPath.row];
    });
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我了解这里发生了什么?

谢谢!

_

编辑

它也像这样工作:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];

    if (detailViewController.view) {
        // do notihing
    }
    detailViewController.detailLabel.text = [_items objectAtIndex:indexPath.row];
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 8

在设置UILabel的值时,视图尚未加载,因此如果您检入调试,您可能会发现detailLabel为nil.

为什么不在自定义init方法中传递值?例如

- (id)initWithDetails:(NSString *)detailsText;
Run Code Online (Sandbox Code Playgroud)

然后在viewDidLoad中,赋值?

根据您的编辑,回答您的问题,为什么它的工作原理:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TestViewController *test = [[testViewController alloc] init];

    NSLog(@"Before: %@", test.label);

    if(test.view){}

    NSLog(@"After: %@", test.label);

    [[test label] setText:@"My Test"];

    [self.navigationController pushViewController:test animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

我已经模拟了你的场景,如果你在ViewController上访问'view',如果视图不存在,它将调用viewDidLoad.所以你的UILabel现在已经定了.这是控制台的输出.

2013-10-11 16:21:04.555 test[87625:11303] Before: (null)
2013-10-11 16:21:04.559 test[87625:11303] After: <UILabel: 0x75736b0; frame = (148 157; 42 21); text = 'Label'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x7573740>>
Run Code Online (Sandbox Code Playgroud)

我建议使用自定义init方法,而不是这种方法 - 上面的例子是一个hack.使用类似这样的东西

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    testViewController *test = [[testViewController alloc] initWithText:@"My Test"];

    [self.navigationController pushViewController:test animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

如果您不想使用自定义初始化程序,我建议您在DetailSController上使用NSString类型的公共属性.在调用init之后使用您的方法分配.然后在DetailViewController中,在viewDidLoad中,或者出现,将IBOutlet设置为包含NSString的属性的值.