我在使用 popToRootViewController 时遇到问题。我有一个 Root View A 和一个 TableView B。当在 TableView 中选择一行时,我将一个字符串传递回 Root View 并取决于该字符串。我更改了 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
再次感谢您的帮助。
最佳答案
你的代码没有问题。当标题更改时,UIButtonTypeSystem
类型的 UIButton
的预期行为会闪烁。
一个简单的解决方法是将按钮的类型设置为 UIButtonTypeCustom
。
编辑: 您可以在 Interface Builder 中更改按钮的类型:
关于ios - popToRootViewController 时 View 闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23922596/