【问题标题】:UIView inside a UIViewController or better way to do it?UIViewController 内的 UIView 还是更好的方法?
【发布时间】:2013-01-08 15:54:30
【问题描述】:

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

下图显示了一个 UIViewController,但视图的第二部分是一个自定义 UIView(带有个人资料图片、名称和显示视图按钮的那个)。 使用此代码分配子类 UIView:

    profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
    profileView.myTripGradientColor = YES;
    [self.view addSubview:profileView];

问题当然是 UIView 上的按钮不能显示任何视图,因为只有 UIViewController 可以将另一个 ViewController 推送到窗口(对吗?)。

UIView 在应用程序的多个地方使用,需要轻松添加,并且在整个应用程序中具有相同的行为。

在我添加按钮之前效果很好,我开始认为我做错了,必须有更好的方法来做到这一点(也许将 UIView 更改为其他东西?)。

我在想我应该可以打电话:

self.superview

然后以某种方式获取 ViewController,这样我就可以将另一个 ViewController 推送到视图层次结构中,但是不行。

关于如何正确执行此操作的任何建议和提示?

更新: 我不知道如何从按钮推送另一个 UIViewController。

在UIView中按下按钮时,这个方法应该怎么做:

- (void) showViewButtonTouched:(UIButton*)sender {
GPProfileSocialFriendsViewController *friendsSettings = [[GPProfileSocialFriendsViewController alloc] init];

}

如何推送 GPProfileSocialFriendsViewController?

【问题讨论】:

  • 由于没有人回复我猜我不是唯一一个不明白你的问题是什么
  • 问题是按钮不能推动另一个视图控制器。我会更新我的问题。

标签: objective-c ios uiview uiviewcontroller uiview-hierarchy


【解决方案1】:

您的- (void) showViewButtonTouched:(UIButton*)sender 方法应该在您的控制器中,并且可能更好地命名为- (void) showView:(UIButton*)sender- (void) showProfile:(UIButton*)sender,以便清楚地表明它的作用(而不是您如何到达那里)。

管理从一个状态到另一个状态的转换不是视图的责任。如果您将方法移至控制器,则问题不再存在(如果您没有这样的导航控制器,您可以轻松访问 self.navigationController 或直接推送:

[self presentViewController:vcThatNeedsToBePushed animated:YES completion:nil];

【讨论】:

  • 这可能是我的问题中最好的问题。但我真的想让它更动态,所以我不必在每个使用 UIView 的 Viewcontroller 上定义选择器方法。但这可能应该是另一个问题。谢谢:-)
【解决方案2】:

我认为您可以在 UIViewController 上的 GPProfileView 中创建弱引用。像这样:

@property (weak, nonatomic) UIViewController *rootController;

创建 GPProfileView 时,分配 rootController-property:

profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
profileView.rootController = self;   // if view created in view controller
[self.view addSubview:profileView];

在按钮选择器的实现中:

self.rootController push... // or pop

这可能不正确,但你可以试试

【讨论】:

    【解决方案3】:

    您可以让视图控制器在按下按钮时推送下一个视图控制器。视图控制器可以在按钮上添加一个目标/动作,以便在 touch up inside 事件上调用视图控制器中的动作方法。

    【讨论】:

      猜你喜欢
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-03
      相关资源
      最近更新 更多