0

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

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

UIViewController 内的 UIView 的图像,UIView 上有一个按钮

4

3 回答 3

1

我认为您可以在 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

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

于 2013-01-08T16:11:37.287 回答
1

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

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

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

于 2013-01-08T16:31:26.563 回答
0

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

于 2013-01-08T16:55:25.053 回答