0

我遇到了 popToRootViewController 的问题。我有一个根视图 A 和一个表视图 B。当在表视图中选择一行时,我将一个字符串传递回根视图,具体取决于字符串。我更改了 A 上按钮的标题。

我制作了一个非常简单的版本,并将其放在 gitHub 上,链接如下:https ://github.com/spennyf/didSelect_test 。我这样做是因为很难解释,直到您在模拟器的手机上实际运行它。并看到闪光。我不知道它为什么会发生或如何解决它。任何帮助将不胜感激,我还将在下面发布大部分代码,但如果您能自己看到 flash,我认为这将有助于解释问题。继承人的代码:

视图控制器A.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIButton *btn1;

@property (strong, nonatomic) IBOutlet UIButton *btn2;

@property (strong, nonatomic) IBOutlet NSString *object;

@end

视图控制器A.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated {
    NSLog(@"object: %@", _object);
    if ([_object isEqualToString:@"object1"]) {
        [_btn1 setTitle:@"new1" forState:UIControlStateNormal];
    }
    if ([_object isEqualToString:@"object2"]) {

        [_btn2 setTitle:@"new2" forState:UIControlStateNormal];
    }

}

@end

表视图B.m

#import "TableViewController.h"
#import "ViewController.h"

@interface TableViewController () {
    NSMutableArray *_objects;

}


@end

@implementation TableViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    _objects = [[NSMutableArray alloc] init];

    NSDictionary *obj1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"object1", @"title", nil];
    NSDictionary *obj2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"object2", @"title", nil];
    // NSDictionary *obj3 = [[NSDictionary alloc] initWithObjectsAndKeys:@"rpi", @"title", nil];

    [_objects addObject:obj1];
    [_objects addObject:obj2];



}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return _objects.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    //NSLog(@"%@", _objects);
    cell.textLabel.text = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];

    return cell;
}

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

    ViewController *myController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0];
    myController.object = [[_objects objectAtIndex:indexPath.row] objectForKey:@"title"];
    [self.navigationController popToRootViewControllerAnimated:YES];

}


@end

再一次感谢你的帮助。

4

1 回答 1

2

您的代码没有问题。当其标题更改时UIButton,类型为闪烁的 s 的预期行为。UIButtonTypeSystem

一个简单的解决方法是将按钮的类型设置为UIButtonTypeCustom.

编辑:您可以在界面生成器中更改按钮的类型:

在 Interface Builder 中更改按钮类型

于 2014-05-28T22:34:09.057 回答