1

とを持ってAppDelegateMainViewControllerます。で作成され、背景画像を設定し、に表示する必要がありMainViewControllerます。それはほとんどそれですが、私は見ることができません。UIButtonAppDelegateMainViewControllerUIButton

位置合わせのためにIBOutlet、IBでボタンを作成して接続しました。

MainViewController.h

@property (strong, nonatomic) IBOutlet UIButton *searchBtn;

MainViewController.m

@synthesize searchBtn;

AppDelegate.m

@synthesize mainVC;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ViewController > setup:
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mainVC = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainVC;
    self.window.backgroundColor = [UIColor whiteColor];

    // Buttons > Set background images:
    [self.mainVC.searchBtn setImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];

    [self.mainVC.view addSubview:self.mainVC.searchBtn];

    [self.window makeKeyAndVisible];

    return YES;
}
4

2 に答える 2

6

IBOutletまず、プログラムで作成する場合は必要ありません。

次に、ボタンの作成を、viewDidLoadではなくViewControllerに移動することをお勧めしますAppDelegate。それはあなたのビューコントローラーの仕事です。

alloc init第三に、プログラムでボタンを作成する場合は、おそらくボタンを使用する必要があります。

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(...)];
[button setBackgroundImage:someImage];
[button addTarget:self action:@selector(something:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

そんな感じ。

于 2012-08-07T17:18:59.553 に答える
1

最初にAppDelegateではなく、MainViewControllerクラスにボタンを追加する必要があります。

また、AppDelegateではなく、MainViewControllerでビューの背景を設定する必要があります。

次のようになります。

AppDelegate.m

@synthesize mainVC;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ViewController > setup:
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mainVC = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainVC;

    [self.window makeKeyAndVisible];

    return YES;
}

MainViewController.mのviewDidLoadにこれを入れます

- (void)viewDidLoad {
    [super viewDidLoad];
    // Buttons > Set background images:
    [searchBtn setBackgroundImage:[UIImage imageNamed:@"search.png"] forState:UIControlStateNormal];

    //If you adding the button in Interface Builder you don't need to add it again
    //[self.view addSubview:self.mainVC.searchBtn];
}
于 2012-08-07T17:19:32.170 に答える