Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using presentViewController from UIView

I am creating a title bar for my iOS application and I am making this inside a UIView. The only issue I am having is with the "home button". When the home button is pressed, it needs to go to the home page, which is ViewController.

However, it doesn't look as if [self presentViewController:vc animated:NO completion:NULL]; works for UIView.

How do I work around this problem ?

- (void) createTitlebar {

    CGRect titleBarFrame = CGRectMake(0, 0, 320, 55);

    //Create the home button on the top right of the page. When clicked, it will navigate to the home page of the application
    homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    homeButton.frame = CGRectMake(275, 10, 40, 40);
    [homeButton setTag:1];
    [homeButton setImage:[UIImage imageNamed:@"homeIcon.png"] forState:UIControlStateNormal];
    [homeButton addTarget:self action:@selector(homeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:homeButton];
}

- (IBAction)homeButtonPressed:(id)sender{

    //Transition to the submit button page
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:vc animated:NO completion:NULL];
}
like image 802
Ashish Agarwal Avatar asked Mar 25 '13 19:03

Ashish Agarwal


People also ask

How do I present a UIView in Swift?

present the gray UIView like you would usually present a ViewController (appearing bottom up and user can slide down to dismiss). The bottomBar is a ContainerView and should not change by switching between the VC's, only the gray UIView which you can see in the 2nd picture.

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.


5 Answers

You can do this without using the delegate pattern. Here you go

ObjectiveC

UIViewController *currentTopVC = [self currentTopViewController];
currentTopVC.presentViewController......... 

- (UIViewController *)currentTopViewController {
    UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}

Swift

var topVC = UIApplication.sharedApplication().keyWindow?.rootViewController
while((topVC!.presentedViewController) != nil) {
     topVC = topVC!.presentedViewController
}
topVC?.presentViewController........
like image 98
Shamsudheen TK Avatar answered Oct 04 '22 14:10

Shamsudheen TK


Only UIViewController can present another view controller, so if you need to show a viewcontroller from view there are several way, one of them is like this:

make a viewcontroller on which your parent view is situated a delegate of it

ParentView *view = [ParentView new];
...
view.delegate = self;

then, inside ParentView call method of that delegate

- (IBAction)homeButtonPressed:(id)sender {
    [self.delegate buttonPressed];
}

and then, inside your VC implement

 -(void)buttonPressed {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:vc animated:NO completion:NULL];
}

If you need to keep this code inside UIView and avoid delegation you can do a trick like this (personally i don't like it but it should work)

-(void)buttonPressed{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    ViewController *vc = [storyboard      instantiateViewControllerWithIdentifier:@"ViewController"];
    [vc setModalPresentationStyle:UIModalPresentationFullScreen];
    [(UIViewController*)self.nextResonder presentViewController:vc animated:NO completion:NULL];
}
like image 41
alex Avatar answered Oct 04 '22 13:10

alex


Here's a more idiomatic Swift 3 version of Shamsudheen's answer:

extension UIApplication {

    static func topViewController() -> UIViewController? {
        guard var top = shared.keyWindow?.rootViewController else {
            return nil
        }
        while let next = top.presentedViewController {
            top = next
        }
        return top
    } 
}

Then you can just call:

UIApplication.topViewController()?.present(...)
like image 34
Hans Brende Avatar answered Oct 04 '22 13:10

Hans Brende


With Swift 4 - 4.2 Adding onto Shamsudheen TK's answer.

var topVC = UIApplication.shared.keyWindow?.rootViewController
    while((topVC!.presentedViewController) != nil) {
        topVC = topVC!.presentedViewController
    }
    let customViewController = CustomViewController()
    topVC?.present(customViewController, animated: true, completion: nil)

With UINavigationController: Here is also an additional feature -> You can pass along a UINavigationController with your customViewController.

 var topVC = UIApplication.shared.keyWindow?.rootViewController
    while((topVC!.presentedViewController) != nil) {
        topVC = topVC!.presentedViewController
    }
    let customViewController = CustomViewController()
    let navController = UINavigationController(rootViewController: CustomViewController)
    topVC?.present(navController, animated: true, completion: nil)
like image 26
N. Der Avatar answered Oct 04 '22 12:10

N. Der


You can try below code

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
 ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
 UIViewController *vc1 = [UIApplication sharedApplication].keyWindow.rootViewController;
 [vc1 presentViewController:vc animated:YES completion:nil]; 
like image 36
Hardik Thakkar Avatar answered Oct 04 '22 12:10

Hardik Thakkar