我对如何正确执行某种操作有疑问。
下图显示了一个 UIViewController,但 View 的第二部分是自定义 UIView(带有个人资料图片、名称和“显示 View ”按钮的部分)。 使用此代码分配子类 UIView:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
[self.view addSubview:profileView];
问题当然是 UIView 上的按钮不能显示任何 View ,因为只有 UIViewController 可以将另一个 ViewController 推送到窗口(对吗?)。
UIView 在应用中的多个位置使用,需要轻松添加并在整个应用中具有相同的行为。
在我添加按钮之前效果很好,我开始认为我做错了,必须有更好的方法来做到这一点(也许将 UIView 更改为其他东西?)。
我在想我应该可以打电话:
self.superview
然后以某种方式获取 ViewController,这样我就可以将另一个 ViewController 插入 View 层次结构,但是不行。
关于如何正确执行此操作的任何建议和提示?
更新: 我不知道如何从按钮中推送另一个 UIViewController。
当按下 UIView 中的按钮时,我应该在这个方法中做什么:
- (void) showViewButtonTouched:(UIButton*)sender {
GPProfileSocialFriendsViewController *friendsSettings = [[GPProfileSocialFriendsViewController alloc] init];
}
如何推送 GPProfileSocialFriendsViewController?
最佳答案
你的 - (void) showViewButtonTouched:(UIButton*)sender
方法应该在你的 Controller 中并且可能更好地命名为 - (void) showView:(UIButton*)sender
或 - (void) showProfile:(UIButton*)sender
所以它清楚地表明它做了什么(而不是你如何到达那里)。
管理从一种状态到另一种状态的转换不是 View 的责任。如果将方法移至 Controller ,问题就不再存在(如果没有这样的导航 Controller ,您可以轻松访问 self.navigationController
或直接推送:
[self presentViewController:vcThatNeedsToBePushed animated:YES completion:nil];
关于objective-c - UIViewController 中的 UIView 或更好的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14218920/