سؤال

I have a problem on how to properly do a certain kind of action.

The image below shows a UIViewController, but the second part of the view is a custom UIView (the one with the profile pic, name and Show View button). The subclassed UIView is allocated using this code:

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

The problem is of course, that the button on the UIView can't show any view, since it's only the UIViewController that can push another ViewController to the window(correct?).

The UIView is used in several places in the app and needs to be added easily and have the same behavior across the application.

This worked great until I added the button, and I'm starting to think I've made this wrong, and there has to be a better way to do it (maybe change the UIView to something else?).

I was thinking I should be able to call:

self.superview

And then somehow get the ViewController so I can push another ViewController into the view hierarchy, but nope.

Any suggestions and a tips on how to do this correctly?

UPDATE: I have no idea on how to push another UIViewController from the button.

What should I do in this method when pressing the button in the UIView:

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

}

How do I push GPProfileSocialFriendsViewController?

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

هل كانت مفيدة؟

المحلول

Your - (void) showViewButtonTouched:(UIButton*)sender method should be in your controller and would probably be better named - (void) showView:(UIButton*)sender or - (void) showProfile:(UIButton*)sender so it clearly denotes what it does (not how you got there).

It's not the view's responsibility to manage transitions from a state to another. If you move your method to your controller, your problem is no more (you can easily access self.navigationController or push directly if you don't have an navigation controller like this:

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

نصائح أخرى

I think you can create weak reference in GPProfileView on UIViewController. Like this:

@property (weak, nonatomic) UIViewController *rootController;

when you create GPProfileView, assign 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];

and in implementation of button selector:

self.rootController push... // or pop

May be this not correct, but you can try

You could let the view controller push the next view controller when the button is pushed. The view controller can add a target/action on the button, so that the action method in the view controller is called on the touch up inside event.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top