0

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; } 

2 Answers 2

2

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]; } 
Sign up to request clarification or add additional context in comments.

Comments

1

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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.