How to add a Control by code

The header looks like this:

#import <UIKit/UIKit.h>
@class UntitledViewController;

@interface UntitledAppDelegate : NSObject <UIApplicationDelegate> {
	IBOutlet UIWindow *window;
	UIButton *button;
}
@property (nonatomic, retain) UIWindow *window;
-(void)myaction:(id)sender;
@end

Then I added your code to the app delegate:

- (void)applicationDidFinishLaunching:(UIApplication *)application {	

	CGRect btnRect = CGRectMake(30, 160, 100, 20);
	// Initialization code
	button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
	button.frame = btnRect; // size and position of button
	[button setTitle:@"Sound_1" forState:UIControlStateNormal];
	button.backgroundColor = [UIColor clearColor];
	button.adjustsImageWhenHighlighted = YES; 

	//Add action handler and set current class as target
	[button addTarget:self action:@selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
	[window addSubview:button];
    [window makeKeyAndVisible];
}
-(void)myaction:(id)sender {
	NSLog(@"myaction myaction called");
}
//....

=============================================

– (void)viewWillAppear:(BOOL)animated {
tabBar =[[UITabBar alloc] initWithFrame:CGRectMake(0,370,320,50)];
tabBar.backgroundColor =[UIColor blackColor];

UITabBarItem *search =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
UITabBarItem *recents =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:1];
UITabBarItem *favorites =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
tabBar.items =[[NSArray arrayWithObjects:search,recents,favorites, nil] retain];
tabBar.selectedItem = search;
[self.view addSubview:tabBar];
[tabBar release];
}

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: