0

I have a project, which consists of the standard AppDelegate and ViewController files, but does not utilize nib files.

Here's the code:

delegate header

// AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *vc1;
@end

delegate implementation

// AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.vc1 = [[ViewController alloc] init];
    self.window.rootViewController = self.vc1;
    [self.window makeKeyAndVisible];
    return YES;
}
@end

view controller header

//  ViewController.h
#import <UIKit/UIKit.h>

@interface view1 : UIViewController{
    UIButton *button;
}
@end

view controller implementation

//  ViewController.m
#import "ViewController.h"

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect buttonFrame = CGRectMake(30, 30, 100, 30);
    button = [[UIButton alloc] initWithFrame: buttonFrame];
    [button setTitle: @"Switch View" forState: UIControlStateNormal];
    [self.view addSubview: button];

    [button addTarget: self action: @selector(buttonClicked:) forControlEvents: UIControlEventTouchDown];
}

- (void) buttonClicked: (id)sender {
    SecondViewController *vc2 = [[SecondViewController alloc] init];
        //both methods throw the same error - no known class method for selector
    //[self presentViewController:vc2 animated:YES completion:nil];         
    [self presentModalViewController:vc2 animated:YES completion:nil];
}

@end

When trying to load another nib-less view controller (in the buttonClicked method above), it keeps throwing the error

No known class method for selector 'presentViewController'

What am I doing wrong?

1 Answer 1

2

The right method is the following:

[self presentViewController:vc2 animated: YES completion:nil];
Sign up to request clarification or add additional context in comments.

3 Comments

It works! Thank you very much. This works since it's in a method - would it be possible to run this in a normal function instead?
what do you mean by normal function?
I just tried it and it works too. Thank you. I meant using a function instead of an Objective-C method: (void) buttonClicked {...} and not (void) buttonClicked: (id)sender {...}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.