popToRootViewController时查看闪烁

时间:2014-05-28 21:59:42

标签: ios objective-c flash uitableview poptoviewcontroller

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

我已经制作了一个非常简单的版本,并将其放在gitHub的链接上:https://github.com/spennyf/didSelect_test。我这样做是因为在你真正在模拟器的手机上运行它之前很难解释。看到闪光灯。我不知道为什么会发生这种情况或如何解决它。任何帮助将不胜感激我将发布下面的大部分代码,但如果你能亲眼看到闪存,我认为这将有助于解释问题。下面是代码:

viewControllerA.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

viewControllerA.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

tableviewB.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

再次感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您的代码没有问题。类型UIButton的{​​{1}}在其标题发生更改时会闪烁。

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

编辑: 您可以在Interface Builder中更改按钮的类型:

Changing the button type in Interface Builder