UIButton を作成できます。
ただし、 callback: target: object は現在のオブジェクト内では機能しません。??
「self.viewController」オブジェクトしか実行できず、現在のオブジェクトに対して「self」を実行することはできません。
どうも
#import <Foundation/Foundation.h>
#import "ViewController.h"
@class ViewController;
@interface CGuiSetup : NSObject
{
@public
ViewController *viewController;
}
- (void) Setup;
- (void) ButtonRespond:(UIButton*) btn;
@end
#import "CGuiSetup.h"
@implementation CGuiSetup
- (void) ButtonRespond:(UIButton*) btn
{
NSLog(@"ButtonRespond");
}
- (void) Setup
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10,10,100,100)];
// But I want to call the following in the CGuiSetup object (self) instead... but it crashes out if I leave it as just "self"
[btn addTarget:self.viewController action:@selector(ButtonRespond:)
forControlEvents:UIControlEventTouchUpInside]; //works: for "self.viewController" if I put ButtonRespond in the ViewController
[btn addTarget:self action:@selector(ButtonRespond:)
forControlEvents:UIControlEventTouchUpInside]; //fails: for "self"
[self.viewController.view addSubview:btn];
}
@end
#import <UIKit/UIKit.h>
#import "CGuiSetup.h"
@class CGuiSetup;
@interface ViewController : UIViewController
{
CGuiSetup *guiSetup; //<---- had to take this out of the "viewDidLoad" method
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
guiSetup = [CGuiSetup alloc];
guiSetup->viewController = self;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(10,10,100,100)];
[btn addTarget:guiSetup action:@selector(ButtonRespond:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}