如何在加载应用程序时执行代码/功能?
答案 0 :(得分:0)
我假设您想在调用app委托之前执行一些代码 - main函数是应用程序的入口点...您可以在main中编写代码或调用函数。
答案 1 :(得分:0)
您可以获得的最接近的是在应用程序在应用程序委托中的相应方法中完成启动后直接放置代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
int randomNumber = arc4random_uniform(100);
NSLog(@"A random number between 0 and 100: %i", randomNumber);
// Override point for customization after application launch.
return YES;
}
- 或 -
在app委托变为实例化之前,将代码放在main.m文件中:
int main(int argc, char * argv[])
{
@autoreleasepool {
// PUT YOUR CODE HERE!
return UIApplicationMain(argc, argv, nil, NSStringFromClass([BCAppDelegate class]));
}
}
根据评论更新您的具体问题:
此代码适用于我:这是UIViewController实现文件的完整代码!
//
// WHEFirstViewController.m
// a
//
// Created by Ben Marten on 10.06.14.
// Copyright (c) 2014 Benjamin Marten. All rights reserved.
//
#import "WHEFirstViewController.h"
@implementation WHEFirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100.f, 100.f, 100.f, 100.f)];
[button setTitle:@"Randomly Located Button" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor redColor]];
int x = arc4random_uniform(320);
int y = arc4random_uniform(576);
[button setCenter: CGPointMake(x,y)];
[self.view addSubview:button];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end