求用纯代码做界面的源代码

iOS 码拜 10年前 (2015-05-09) 759次浏览 0个评论
 

如题。

40分
#import <UIKit/UIKit.h>
@class UITextView;

@interface HelloController : UIViewController <UITabBarDelegate>
{
	int value;
}
@end

@implementation HelloController

- (id)init
{
	if (self = [super init])
	{
		self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
	}
	return self;
}

- (void) goRed
{
	self.view.backgroundColor = [UIColor colorWithRed: 1.0f green:0.45f blue:0.45f alpha:1.0f];
}

- (void) goBlue
{
	self.view.backgroundColor = [UIColor colorWithRed: 0.45f green:0.45f blue:1.0f alpha:1.0f];
}

- (void)loadView
{
	// Set up the text view to show the current value
	UITextView *contentView = [[UITextView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

	contentView.editable = NO;
	contentView.textAlignment = UITextAlignmentCenter;
	contentView.font = [UIFont fontWithName:@"American Typewriter" size:120];

	contentView.autoresizesSubviews = YES;
	contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

	self.view = contentView;
    [contentView release]; 

	// Initialize at 50
	[(UITextView *)self.view setText:@"\n50"];
	value = 50;

	// The app style is black
	self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

	NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:4];

	UIBarButtonItem *flexibleSpaceItem;
	flexibleSpaceItem = [[[UIBarButtonItem alloc] 
						  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
						  target:nil action:NULL] autorelease];
	[buttons addObject:flexibleSpaceItem];
	[flexibleSpaceItem release];

	UIBarButtonItem *item;
	item = [[UIBarButtonItem alloc] 
			initWithImage:[UIImage imageNamed:@"down.png"]
			style:UIBarButtonItemStylePlain 
			target:self 
			action:@selector(decrement:)];
	[buttons addObject:item];
	[item release];

	item = [[UIBarButtonItem alloc] 
			initWithImage:[UIImage imageNamed:@"up.png"]
			style:UIBarButtonItemStylePlain target:self 
			action:@selector(increment:)];
	[buttons addObject:item];
	[item release];

	flexibleSpaceItem = [[[UIBarButtonItem alloc] 
						  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
						  target:nil action:NULL] autorelease];
	[buttons addObject:flexibleSpaceItem];
	[flexibleSpaceItem release];

	UIToolbar *toolbar = [[UIToolbar alloc] init]; 
	toolbar.barStyle = UIBarStyleBlackOpaque;
	[toolbar setItems:buttons animated:YES];
	[toolbar sizeToFit];

	self.navigationItem.titleView = toolbar;
	[toolbar release];

	// Add a left button
	self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]
											 initWithTitle:@"Red" 
											 style:UIBarButtonItemStylePlain 
											 target:self 
											 action:@selector(goRed)] autorelease];

	// Add a right button
	self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
											  initWithTitle:@"Blue" 
											  style:UIBarButtonItemStylePlain 
											  target:self 
											  action:@selector(goBlue)] autorelease];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

-(void) increment: (id) sender
{
    [(UITextView *)self.view setText:[NSString stringWithFormat:@"\n%d", ++value]];
}

-(void) decrement: (id) sender
{
    [(UITextView *)self.view setText:[NSString stringWithFormat:@"\n%d", --value]];
}
@end


@interface SampleAppDelegate : NSObject <UIApplicationDelegate> 
@end
@implementation SampleAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
	UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[HelloController alloc] init]];
	[window addSubview:nav.view];
	[window makeKeyAndVisible];
}
@end

int main(int argc, char *argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	int retVal = UIApplicationMain(argc, argv, nil, @"SampleAppDelegate");
	[pool release];
	return retVal;
}

这种的?

谢谢你的回答。

我现在要忙着调查UIWebView的缓存,等我有时间再确认下。

这个问题就这么关掉了,想来不会又什么问题。

其实我更想要一整个工程,这样我可以顺便学习一下结构。iphone的例子大多都是用IB做的,没什么可以参考。

路过,看看

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求用纯代码做界面的源代码
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!