在 XIB iOS 之间移动

发布于 2024-12-06 05:53:04 字数 436 浏览 1 评论 0原文

我有一个基于视图的应用程序,包含三个 xib 文件,每个文件都有自己的视图控制器。我如何从一种更改为另一种?我使用它从 xib 1 移动到 xib 2,但是当我使用相同的代码从 xib 2 移动到 xib 1 时,我在 [selfpresentModal....] 行上得到 EXC_BAD_ACCESS。

MapView *controller = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];

controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];

我怎样才能自由地从一个厦门国际机场转移到另一个厦门国际机场?

I have a view-based application with three xib files, each with its own view controllers. How do I change from one to another? I use this to move from xib 1 to xib 2, but when I use the same code to move from xib 2 to xib 1, i get a EXC_BAD_ACCESS on the [self presentModal....] line.

MapView *controller = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];

controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

[self presentModalViewController:controller animated:YES];

How can I freely move from one xib to another?

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

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

发布评论

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

评论(3

漆黑的白昼 2024-12-13 05:53:04

我认为你想做的是呈现一个模态视图然后将其驳回,对吗?如果是这种情况,那么您将下面的代码放入您用来关闭它的方法中(例如 -(IBAction)dissmissModalView)

[self.parentViewController dismissModalViewControllerAnimated:YES];

希望它有效。让我知道。

What I think you are trying to do is is present a modal view and then dismiss it, right? If that is the case then you put the code below in the method that you use to dismiss it(e.g. -(IBAction)dissmissModalView)

[self.parentViewController dismissModalViewControllerAnimated:YES];

Hopefully that works. Let me know.

何止钟意 2024-12-13 05:53:04

initWithNibName 并不是真正必要的......您可以将其更改为 nil。

所以,这里是正确的代码(没有动画):

MapView *mapView = [[MapView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:mapView animated:NO];

当尝试使用present返回到视图1时,您不应该收到EXC_BAD_ACCESS。如果您无法解决它,只需使用它:

[self dismissModalViewControllerAnimated:YES];

第二个视图控制器将消失,第一个视图控制器将再次可见。

initWithNibName isn't really necessary... you can change that to nil.

So, here is the correct code (without animation):

MapView *mapView = [[MapView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:mapView animated:NO];

You should not be receiving EXC_BAD_ACCESS when trying to go back to view 1 using present. If you cannot resolve it, just use this instead:

[self dismissModalViewControllerAnimated:YES];

The second view controller will disappear and the first view controller will be visible again.

神经暖 2024-12-13 05:53:04

请注意,像此处的其他答案一样呈现模式视图控制器将意味着您拥有不断累积的视图控制器堆栈。使用该应用程序足够长的时间就会崩溃。

相反,您可以从应用程序窗口中交换视图。以下是实现此目的的一种方法:

向应用程序委托添加一个数据成员以存储当前视图:

@class MyAppDelegate : NSObject <...>
{
    UIViewController* currentVC;
}

并在其中添加一条消息以交换 VC:

-(void)setCurrentVC:(UIViewController*)newVC
{
    if (newVC==currentVC) return;
    if (currentVC!=nil)
        [currentVC.view removeFromSuperview];
    currentVC = newVC;
    if (newVC!=nil)
        [self.window addSubview:newVC.view];
 }

以及从一个屏幕交换到另一个屏幕:

MapView* mapView = [[MapView alloc] init];
[[[UIApplication shared] delegate] setCurrentVC:mapView];

Note that presenting modal view controllers like the other answers here will mean that you have an ever-accumulating stack of view controllers. Use the application long enough and it will crash.

Instead, you can swap out the view from the application's window. Here's one way of doing that:

Add a data member to your app delegate to store the current view:

@class MyAppDelegate : NSObject <...>
{
    UIViewController* currentVC;
}

and add a message there to swap VCs:

-(void)setCurrentVC:(UIViewController*)newVC
{
    if (newVC==currentVC) return;
    if (currentVC!=nil)
        [currentVC.view removeFromSuperview];
    currentVC = newVC;
    if (newVC!=nil)
        [self.window addSubview:newVC.view];
 }

and to swap from one screen to another:

MapView* mapView = [[MapView alloc] init];
[[[UIApplication shared] delegate] setCurrentVC:mapView];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文