我无法弄清楚为什么这段代码很糟糕.我认为在dictionaryWithObjectsAndKeys中使用静态字符串是可以的,但它在运行时因EXC_BAD_ACCESS而失败.我使用调试器来识别它在定义行中实际上是失败的.它永远不会输出"Made it here".
- (NSString *)polyName:(NSUInteger)vertices {
NSLog(@"looking for polyName with %d vertices.", vertices);
NSDictionary *polNameDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"triangle", @"3",
@"square", @"4",
@"pentagon","5",
@"Hexagon", @"6",
@"Heptagon", @"7",
@"Octagon", @"8",
@"Nonagon", @"9",
@"Decagon", @"10",
@"Hendecagon", @"11",
@"Dodecagon", @"12",
nil];
NSLog(@"Made it here");
NSString *key = [NSString stringWithFormat: @"%d", vertices];
NSString *polName = [polNameDict objectForKey:key];
return (polName);
// Memory management: No need to release polNameDict, key, polName because they use
// convenience functions
}
Run Code Online (Sandbox Code Playgroud) 我从NavigationViewController模板开始我的xcode项目.现在,当我按下视图时,该视图默认情况下会出现一个编辑按钮而没有后退按钮.我已经注释掉editButton代码和相应的setEditing委托方法.但是我无法显示后退按钮.我究竟做错了什么?
推动新视图:
PlaylistViewController *playlistViewController = [[PlaylistViewController alloc] init];
playlistViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:playlistViewController animated:YES];
[playlistViewController release];
Run Code Online (Sandbox Code Playgroud)
在我的PlaylistViewController中:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"";
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
// doesn't matter if this is here or not
self.navigationItem.hidesBackButton = false;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
Run Code Online (Sandbox Code Playgroud)