objective-c - UIViewController 中的 UIView 或更好的方法吗?

标签 objective-c ios uiview uiviewcontroller uiview-hierarchy

我对如何正确执行某种操作有疑问。

下图显示了一个 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?

An image of a UIView inside a UIViewController, the UIView has a button on it

最佳答案

你的 - (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/

相关文章:

ios - 在 UIViewController 中嵌入 UIPageViewController 停止分页

ios - UIImageview 未缩放

ios - 对于非英语歌曲,如何使用 # 显示看起来像 iPod 音乐的索引

ios - 如何找到应用程序/网站的深层链接?

ios - Airplay:在外部窗口上镜像 subview

ios - 在 ios 中放置阴影

iphone - 在多个矩形中绘制一个 NSString

c# - 我可以在 Xamarin、C#、iOS 中保存对象数组(或 json 数组、字典数组)吗?

ios - Swift:在另一个 UIView 下对 UIView 进行 hitTest

ios - 给 UIView 添加斜角效果