本文介绍了如何用另一个 UIView 替换 self (UIView)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击视图A有一个按钮,我们去视图B

View A has a button upon clicking it, we go to view B

  • 视图 B 不保留指向视图 A 的指针.
  • 从视图 B,我想(以编程方式)加载回视图 A

实际上,我想杀死 B 并用 A 替换它.

Effectively, i'd like to kill B and replace it with A.

我认为以下应该有效,但没有

I was thinking that the following should work but, it does not

从视图 B 调用

ViewController *main = [ViewController new];
[self addSubview:[main view]];

请问我遗漏了什么?

推荐答案

我个人认为最简单的方法是让 UIViewController 和一个 IBOutlet两个 UIView 对象.您可以在界面构建器中添加和设计它们,只需将其中一个(视图 B)设置为隐藏(它是 UIView 中的一个属性).然后,您可以指定一个按钮操作来切换视图 B 的可见性.

Personally I think the easiest way to do this would be by having a UIViewController with an IBOutlet to both UIView objects. You can add and design them both in the interface builder and just set one of them (view B) as hidden (it's a property in UIView).Then, you could specify a button action to toggle the visibility of view B.

我必须补充一点,有一些用于实现屏幕流的结构,例如 NavigationController.但是,在您的情况下,您也可以考虑使用 presentModalViewController:animated: 方法.

I must add though that there are constructs for implementing screen flows, such as the NavigationController. In your case, however, you might also consider the use of the presentModalViewController:animated: method.

这完全取决于实际情况,但一般来说,更好的做法是为应用程序中的每个 UIView 制作一个单独的 UIViewController.

It all depends really, but in general it's better practice to make a seperate UIViewController for each UIView in your application.

希望这会有所帮助!

这篇关于如何用另一个 UIView 替换 self (UIView)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:37