当我更改 UILabel 的文本时屏幕不会更新
Screen doesn't update when I change a UILabel's text
我的环境是 Xcode 6.3,目标是 iOS8.3 for iPad。
我正在使用 Xcode 的模板学习主从视图。当我尝试在详细视图中设置标签文本时出现了一些奇怪的问题:
如果我将更改标签文本的语句放在 P1 位置,它就可以工作。
但是如果我把语句放在P2位置,就不行了
在这两种情况下,detailDescriptionLabel.text 都发生了变化,但实际上只有 P1 语句更新了屏幕。(我已经检查过函数 configureView() 被调用了两次)
有人可以帮助我吗?非常感谢。
下面是我的代码片段:
MasterController.m:
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController ()
@property (copy, nonatomic) NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path = [[NSBundle mainBundle] pathForResource:@"PresidentList" ofType:@"plist"];
NSDictionary *presidentInfo = [NSDictionary dictionaryWithContentsOfFile:path];
self.objects = [NSMutableArray arrayWithArray:[presidentInfo objectForKey:@"presidents"]];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
//P1
//controller.detailItem = (self.objects[indexPath.row])[@"url"];
}
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = (self.objects[indexPath.row])[@"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//P2
//self.detailViewController.detailItem = (self.objects[indexPath.row])[@"url"];
}
@end
DetailController.m:
#import "DetailViewController.h"
@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = self.detailItem;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
barButtonItem.title = NSLocalizedString(@"Master", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = pc;
}
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
@end
我猜你正在使用 segues,这就是使用专用函数 (prepareForSegue
) 的意义所在,指定适当地在创建视图目标视图后初始化值。
除非您在 didSelectRowAtIndexPath
中自己实例化并显示视图,否则将某些内容设置为这样的内容毫无意义 viewcontroller。
我猜 self.detailViewController 是零。因此P2将不起作用。
如果没有看到所有代码很难说,但在 prepareForSegue 中,您可能想在那里分配它。
我的环境是 Xcode 6.3,目标是 iOS8.3 for iPad。
我正在使用 Xcode 的模板学习主从视图。当我尝试在详细视图中设置标签文本时出现了一些奇怪的问题:
如果我将更改标签文本的语句放在 P1 位置,它就可以工作。
但是如果我把语句放在P2位置,就不行了
在这两种情况下,detailDescriptionLabel.text 都发生了变化,但实际上只有 P1 语句更新了屏幕。(我已经检查过函数 configureView() 被调用了两次)
有人可以帮助我吗?非常感谢。
下面是我的代码片段:
MasterController.m:
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController ()
@property (copy, nonatomic) NSMutableArray *objects;
@end
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path = [[NSBundle mainBundle] pathForResource:@"PresidentList" ofType:@"plist"];
NSDictionary *presidentInfo = [NSDictionary dictionaryWithContentsOfFile:path];
self.objects = [NSMutableArray arrayWithArray:[presidentInfo objectForKey:@"presidents"]];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
//P1
//controller.detailItem = (self.objects[indexPath.row])[@"url"];
}
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = (self.objects[indexPath.row])[@"name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//P2
//self.detailViewController.detailItem = (self.objects[indexPath.row])[@"url"];
}
@end
DetailController.m:
#import "DetailViewController.h"
@interface DetailViewController ()
@property (strong, nonatomic) UIPopoverController *masterPopoverController;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.masterPopoverController != nil) {
[self.masterPopoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = self.detailItem;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController:(UIPopoverController *)pc {
barButtonItem.title = NSLocalizedString(@"Master", @"Master");
[self.navigationItem setLeftBarButtonItem:barButtonItem animated:YES];
self.masterPopoverController = pc;
}
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
self.masterPopoverController = nil;
}
@end
我猜你正在使用 segues,这就是使用专用函数 (prepareForSegue
) 的意义所在,指定适当地在创建视图目标视图后初始化值。
除非您在 didSelectRowAtIndexPath
中自己实例化并显示视图,否则将某些内容设置为这样的内容毫无意义 viewcontroller。
我猜 self.detailViewController 是零。因此P2将不起作用。
如果没有看到所有代码很难说,但在 prepareForSegue 中,您可能想在那里分配它。