Reputation: 171
I have a button created using the .xib file. I want the app to autoload the first question (meaning the ViewController to autoload the "showQuestion" method when the app first started. I am a beginner. How do you do it? Please help! Thanks!
ViewController.m
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
//call the init method implemented by the superclass
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
self.questions = @[@"What is your name?",
@"How old are you?",
@"Where are you from?"];
return self;
}
- (IBAction)showQuestion:(id)sender{
self.questionLabel.text = self.questions[currentIndex];
}
the AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
return YES;
}
Upvotes: 0
Views: 64
Reputation: 11233
Try like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Autoload first question
[vc showQuestion:nil];
return YES;
}
ViewController.h
- (IBAction)showQuestion:(id)sender;
Upvotes: 1
Reputation: 3079
In your -viewDidLoad
method of the view controller, just manually call the -showQuestion
method.
i.e. in your ViewController.m, add this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self showQuestion:nil];
}
Upvotes: 2