UIScrollView基本使用。为其它添加多个button ||-iOS objective-c

来源:互联网 发布:mac 网线转接头 编辑:程序博客网 时间:2024/05/18 20:33
 int i;    int n = 0;   //新建一个视图类对象并设置它的大小     UIScrollView *newview =[[UIScrollView alloc]init];    //依次为它的x y位置,长和宽    newview.frame =CGRectMake(0, 0, 300, 400);    //把这个对象加到view中去。显示出来    [self.view addSubview:newview];    //添加10个Button    for(i =0; i<10;i++)    {         //新建一个button对象 button还有一些别的属性比如背景色        //buttonview.backgroundColor = [UIColor clearColor];        UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect];        //为button显示赋值         [buttonview setTitle:@"testbutton" forState:UIControlStateNormal];        //设置button的大小        buttonview.frame = CGRectMake(20, 20+n, 280, 20);        [newview addSubview:buttonview];        //[self.view addSubview:button2];        n = n+30;        NSLog(@"%i",n);            }
//可实现滑动[newview setContentSize:CGSizeMake(320, 420)];

 

    [newview release];

 

下面这段转自:http://my.oschina.net/clownfish/blog/56812

//这里创建一个圆角矩形的按钮    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];     //    能够定义的button类型有以下6种,//    typedef enum {//        UIButtonTypeCustom = 0,          自定义风格//        UIButtonTypeRoundedRect,         圆角矩形//        UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用//        UIButtonTypeInfoLight,           亮色感叹号//        UIButtonTypeInfoDark,            暗色感叹号//        UIButtonTypeContactAdd,          十字加号按钮//    } UIButtonType;    //给定button在view上的位置    button1.frame = CGRectMake(20, 20, 280, 20);    //button背景色    button1.backgroundColor = [UIColor clearColor];    //设置button填充图片    //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];    //设置button标题    [button1 setTitle:@"点击" forState:UIControlStateNormal];    /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/    //以下是几种状态//    enum //        UIControlStateNormal       = 0,         常规状态显现             //        UIControlStateHighlighted  = 1 << 0,    高亮状态显现   //        UIControlStateDisabled     = 1 << 1,    禁用的状态才会显现//        UIControlStateSelected     = 1 << 2,    选中状态             //        UIControlStateApplication  = 0x00FF0000, 当应用程序标志时           //        UIControlStateReserved     = 0xFF000000  为内部框架预留,可以不管他            //    };    /*  * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,     * 那么可以去掉这个功能    */    button1.adjustsImageWhenHighlighted = NO;    /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/    button1.adjustsImageWhenDisabled = NO;  /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/    button1.showsTouchWhenHighlighted = YES;    /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是     按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。     触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中     也可以传入其他类的指针*/    [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];    //显示控件    [self.view addSubview:button1];





 //在UIScrollView中添加的button与事件关联。。//UIButtonTypeCustom 这个属性表示不显示按钮    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    [newscrollview addSubview:button];    button.frame = CGRectMake(0, 0, 300, 200);    //button.backgroundColor = [UIColor redColor];    button.tag = 0;    //UIButton *buttonview = [UIButton buttonWithType:UIButtonTypeRoundedRect];    //[buttonview setTitle:[userinfo objectAtIndex:i] forState:UIControlStateNormal];    //关联事件。。butttest    [button addTarget:self action:@selector(butttest:) forControlEvents:UIControlEventTouchUpInside];//通过button.tag  就能与多个button写不同的事件。。-(IBAction)butttest:(id)sender{    NSLog(@"all all all ...\n");    if( 0 == ((UIButton*)sender ).tag)    {        NSLog(@"test ok..\n");    }}

 

 
原创粉丝点击