我有一个小应用程序,从主屏幕上有几个按钮,点击这些按钮加载到一个新视图,这一切都正常:
- (IBAction)showMaps:(id)sender {
MapViewController *viewcontroller = [[MapViewController alloc] initWithNibName:@"MapView" bundle:nil];
[[self view] addSubview:viewcontroller.view];
[viewcontroller release];
}
加载MapView时出现问题我在MapViewController.h文件中创建了一个新的IBAction:
- (IBAction)showHome:(id)sender;
以及MapViewController.m文件中的此操作:
- (IBAction)showHome:(id)sender {
[self.view removeFromSuperview];
}
但是没有快乐,对此有点新手所以任何帮助都不仅仅是欢迎!
答案 0 :(得分:1)
您的showMaps:
方法会创建一个视图控制器,但不会保留它。如果您希望它保持不变,您将需要保留该viewController的所有权。我建议在主视图控制器上添加一个属性 - 包含showMaps:
方法的属性。示例代码如下:
<强> MainViewController.h 强>
@interface MainViewController : UIViewController {
MapViewController * mapViewController;
}
@property (nonatomic, retain) MapViewController * mapViewController;
- (IBAction)showMaps:(id)sender;
@end
<强> MainViewController.m 强>
@implementation MainViewController
@synthesize mapViewController;
- (void)dealloc {
[mapViewController release];
[super dealloc];
}
- (IBAction)showMaps:(id)sender {
self.mapViewController = [[[MapViewController alloc]
initWithNibName:@"MapView"
bundle:nil] autorelease];
[[self view] addSubview:mapViewController.view];
}
@end