直接把-(void)drawRect:(CGRect)myView方法写到ViewController.m下以下面这样吗,可是运行不出来#import"ViewController.h"@interfaceViewController()@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.CGRectrect=CGRectMake(380,200,300,300);UIView*myView=[[UIViewalloc]initWithFrame:rect];[myViewsetBackgroundColor:[UIColorgrayColor]];[self.viewaddSubview:myView];}-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];//Disposeofanyresourcesthatcanberecreated.}-(void)drawRect:(CGRect)myView{intchang=[LengthTextField.textintValue];intkuan=[WidthTextField.textintValue];NSLog(@"chang=%d",chang);NSLog(@"kuan=%d",kuan);//1.获取图形上下文CGContextRefctx=UIGraphicsGetCurrentContext();//2.画四边形CGContextAddRect(ctx,CGRectMake(20,20,chang,kuan));//如果要设置绘图的状态必须在渲染之前//CGContextSetRGBStrokeColor(ctx,1.0,0,0,1.0);//绘制什么类型的图形(空心或者实心).就要通过什么类型的方法设置状态//CGContextSetRGBFillColor(ctx,1.0,0,0,1.0);//调用OC的方法设置绘图的颜色//[[UIColorpurpleColor]setFill];//[[UIColorblueColor]setStroke];//调用OC的方法设置绘图颜色(同时设置了实心和空心)//[[UIColorgreenColor]set];[[UIColorcolorWithRed:1.0green:0blue:0alpha:1.0]set];//3.渲染图形到layer上//空心的CGContextStrokePath(ctx);//实心的//CGContextFillPath(ctx);}@end
通过appDelegate,也不用IB的方式,用代码来实现,为一个ViewController添加一个NavigationController,并且声明一下他的rootViewcontrollerGroupsController*groupsController=[[[GroupsControlleralloc]initWithNibName:nilbundle:nil]autorelease];UINavigationController*navigationController=[[UINavigationControlleralloc]initWithRootViewController:groupsController];[myViewControllerpresentModalViewController:navigationController];