我有一个使用.xib文件创建的按钮。我希望应用程序自动加载第一个问题(这意味着ViewController会在应用程序首次启动时自动加载“ showQuestion”方法。我是初学者。您如何做?请帮忙!谢谢!

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


AppDelegate.m文件

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

最佳答案

在视图控制器的-viewDidLoad方法中,只需手动调用-showQuestion方法。

即在您的ViewController.m中,添加以下内容:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showQuestion:nil];
}

关于ios - 如何在iOS中自动加载按钮的首次点击?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32281487/

10-10 18:33