从后退/完成按钮中删除 UIView

发布于 2024-09-12 07:16:49 字数 612 浏览 6 评论 0原文

我有一个小应用程序,主屏幕上有几个按钮,当单击这些按钮时会在新视图中加载,这一切都工作正常:

- (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];

}

但是没有乐趣,对此有点新手,所以非常欢迎任何帮助!

I have a small app that from the home screen has a few buttons, when clicked these buttons load in a new view, this all works fine:

- (IBAction)showMaps:(id)sender {    

    MapViewController *viewcontroller = [[MapViewController alloc] initWithNibName:@"MapView" bundle:nil];
    [[self view] addSubview:viewcontroller.view];

    [viewcontroller release];

}

The problem comes when the MapView is loaded I have created a new IBAction in the MapViewController.h file:

- (IBAction)showHome:(id)sender;

And also this action within the MapViewController.m file:

- (IBAction)showHome:(id)sender {
    [self.view removeFromSuperview];

}

But no joy, bit of a newbie to this so any help more than welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-09-19 07:16:49

您的 showMaps: 方法创建一个视图控制器,但不保留它。如果您希望该 viewController 保留下来,您将需要保留该 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

Your showMaps: method creates a view controller, but does not retain it. You will need to retain ownership of that viewController if you want it to stick around. I would suggest adding a property on your main view controller - the one that contains the showMaps: method. Example code below:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文