简体   繁体   中英

Pushing a UIViewController from a UIView

I need to push a UIView into my UINavigation controller . I am doing it by

[self.view addSubview:showContactFlow];

And on a button click in UIView I need to push another UIViewController over the UIView . From the UIView I am not able to access self.navigationcontroller How can I do this?

Edit:

I have set the UIView as the view of a new UIViewController I am pushing into, the before mentioned UIViewController . Now I would like to know, how to handle the UIView button event inside its UIViewController , in which's view it is set.

Add a UINavigationController ivar to the UIView and assign it to the main view controller's. Then you should be able to access it from the UIView .

Edit:

Your UIView subclass:

// CustomView.h
@interface CustomView: UIView {
    // ...
    // your variables
    // ...
    UINavigationController *navController;
}

@property (nonatomic, assign) UINavigationController *navController; // assign, because this class is not the owner of the controller

// custom methods

@end

// CustomView.m
@implementation Customview

// synthesize other properties
@synthesize navController;

// implementation of custom methods

// don't release the navigation controller in the dealloc method, your class doesn't own it

@end

Then before the [self.view addSubview:showContactFlow]; line just add [showContactFlow setNavController:[self navigationController]]; and then you should be able to access your hierarchy's navigation controller from your UIView and use it to push other UIViewController s.

You should try to work with an MVC approach. So your controller has access to all that stuff and can keep pushing and popping views, so the view doesn't need to know too much about the controller.

Otherwise, and for this case you can solve it fast by using delegation . So:

showContactFlow.delegate = self;
[self.view addSubview:showContactFlow];

So later in the UIView , you can just say:

[self.delegate addSubview:self];

This is gonna work, but it's not likely to be the best approach you should use.

On button click, you can present a view controller like,

    -(void)buttonFunction{
           ThirdVC *third= [[ThirdVC alloc]initWithNibNme];......

           [self presentViewController:third animated:NO];


    }

Using Core animation you can make NavigationController's pushviewController like animation on writing code in ThirdVC's viewWillAppear: method.

where do you add the UIButton is it in showContactFlow view or in the ViewController's view??

In regard to the modalViewControllers issue the correct method is

    [self presentModalViewController:viewController animated:YES];

the standard animation in upwards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM