IPhone学习笔记四-----实现滑块和标签操作表和警报

来源:互联网 发布:mac版windows系统下载 编辑:程序博客网 时间:2024/05/17 00:05

前面用到了Image View控件和常用Lable和TextField ,Button控件。

1滑块控件

 UILabel *sliderLabel;  

@property (nonatomic,retain)IBOutlet UILabel *sliderLabel;//添加对应控件输出口。

-(IBAction)silderChange:(id)sender;//滑块触发操作

实现

@synthesize sliderLabel;

-(IBAction)silderChange:(id)sender

{

    UISlider *slider=(UISlider *) sender;//首先把sender转成UISlider类型

    int progressAsInt=(int)(slider.value+0.5f);//接受滑块的值并加0.5f

    NSString *newText =[[NSString alloc] initWithFormat:@"%d",progressAsInt];//将滑块值存入新字符串

   sliderLabel.text=newText;//将字符串赋给文本

    [newText release];

}

在视图中添加相应的滑块控件和显示滑块值的标签。设置滑块的最大值和最小值,当前值,从File‘s Owner 连接到标签(输出口),滑块事件用的是Value Changed连接到File’s OWner选择触发的方法。

2实现开关,按钮和分段按钮

#define kSwitchesSegmentIndex0 //常量

   UISwitch *leftSwitch;    //分段控件

    UISwitch *rightSwitch;

    UIButton *doSomethingButton; //按钮控件

@property(nonatomic,retain)IBOutletUISwitch *leftSwitch;

@property(nonatomic,retain)IBOutletUISwitch *rightSwitch;

@property(nonatomic,retain)IBOutletUIButton *doSomethingButton;

-(IBAction)toggleControls:(id)sender;

-(IBAction)switchChanged:(id)sender;

-(IBAction)ButtonPressed;


实现

@synthesize leftSwitch;

@synthesize rightSwitch;

@synthesize doSomethingButton;

-(IBAction)toggleControls:(id)sender//分段调用方法,是隐藏开关显示按钮,或隐藏按钮显示开关

{

   if ([senderselectedSegmentIndex]==kSwitchesSegmentIndex) {

       leftSwitch.hidden=NO;

       rightSwitch.hidden=NO;

       doSomethingButton.hidden=YES;

    } 

    else

    {

       leftSwitch.hidden=YES;

       rightSwitch.hidden=YES;

       doSomethingButton=NO;

    }

}


-(IBAction)switchChanged:(id)sender//有开关被按下时调用,并使用该值来设置两个开关。

{

    UISwitch *whichSwitch=(UISwitch *) sender;

    BOOL setting =whichSwitch.isOn;

    [leftSwitchsetOn:settinganimated:YES];//该方法接受两个bool值,第一个参数确定开关应该打开还是关闭。

    [rightSwitchsetOn:settinganimated:YES];//第二个参数让我们指定按钮应该缓慢地滑动还是应该迅速移动到新位置。

}

-(IBAction)ButtonPressed

{  

}

在视图中添加一个分段控件Segmented和两个开关控件Switch,并且把输出口连接好,因为之后会在上面用一个button控件放在上面,这样就不方便连接。

主要是要如何连接这很讲究的事。


3。操作表和警报

操作表用于迫使用户在两个或多个项之间进行选择。用户只有单击一个按钮之后才能继续使用应用程序。从屏幕底部弹出。

警报迫使用户在继续使用应用程序之前做出响应,更多地用户通知用户发生了一些重要或不正常的事情。可能显示单个按钮,但是如果多个响应更合适,也可以选择显示多个按钮。

显示操作表

-(IBAction)ButtonPressed

{

   //操作表

   UIActionSheet *actionSheet= [[UIActionSheetalloc] initWithTitle:@"Are you sure?"//显示标题

delegate:self//委托,它将在该表上的按钮被按下时收到通知。

cancelButtonTitle:@"No Way"//取消按钮标题

destructiveButtonTitle:@"Yes,I am sure!"//继续按钮

   otherButtonTitles:nil];//表单上显示的其他按钮的数量nil作为最后一个变量传递

    [actionSheet showInView:self.view];

    [actionSheet release];


}


-(void) actionsheet:(UIActionSheet *)actionSheet 

didDismissWithButtonIndex:(NSInteger)buttonIndex

{

    if (buttonIndex !=[actionSheet cancelButtonIndex]) {

        NSString *msg =nil;

       if(nameField.text.length>0)

            msg= [[NSStringalloc] initWithFormat:@"You can breathe easy,%@,everythihg went OK",nameField.text];

        else

            msg=@"You can breathe easy,everythihg went OK";

  //警报的创建和使用方式

        UIAlertView *alert= [[UIAlertViewalloc] initWithTitle:@"Something was done"message:msg delegate:selfcancelButtonTitle:@"Phew!"otherButtonTitles: nil];

        [alert show];

        [alert release];

        [msg release];

    }   

}






原创粉丝点击