我的环境是Xcode 6.3,目标是iPad的iOS8.3。
我使用Xcode的模板学习了Master-Detail视图。当我尝试在详细视图中设置标签文本时出现一些奇怪的问题:
如果我将标签的文本更改为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
答案 0 :(得分:2)
我猜你正在使用segues,而且使用专门指定的专用函数(prepareForSegue
)在创建视图目标视图之后初始化值。
除非您在didSelectRowAtIndexPath
中自己实例化并显示视图,否则无法为这样的viewcontroller设置内容。
答案 1 :(得分:0)
我猜猜self.detailViewController是零。因此P2不起作用。
很难说没有看到所有代码但是在prepareForSegue中,你可能想在那里分配它。