參考前篇的文件,改良原來的實作,增加了一個stepper在第二個alertView上,並讓兩個alertView有不同的大小與位置,形態也不相同。
修改原先的實作,來表現特殊效果如下
1. 加入一個Button及一個label到storyboard上
2. 在mainViewControlller.h上註冊新的元件
#import <UIKit/UIKit.h>
@interface mainViewController : UIViewController
@property (nonatomic) NSString *text;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (nonatomic) UIStepper *stepper2;
@property (weak, nonatomic) IBOutlet UILabel *stepper2Label;
@property (nonatomic) UILabel *insideLabel;
@end
3. 在mainViewController.m加上新的程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
@implementation mainViewController
@synthesize text;
@synthesize textLabel;
@synthesize stepper2;
@synthesize stepper2Label;
@synthesize insideLabel;
int value=0;
int alertNumber = 0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showAlertView:(id)sender {
alertNumber = 0;
[self alertViewAction1];
}
- (IBAction)dataInput:(id)sender {
alertNumber = 1;
[self alertViewAction2];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
if (alertNumber == 0)
text = @"Cancel Button Pressed";
break;
case 1:
text = @"Button 1 Pressed";
break;
case 2:
text = @"Button 2 Pressed";
break;
case 3:
text = @"Button 3 Pressed";
break;
default:
break;
}
if (alertNumber == 0)
textLabel.text = text;
else
stepper2Label.text = text;
}
- (void) willPresentAlertView:(UIAlertView *)alertView{
// alertView的大小位置,只能在此處設定
if (alertNumber ==0)
alertView.frame = CGRectMake(360.0f, 350.0f, 300.0f, 350.0f);
else
alertView.frame = CGRectMake(250, 650.0f, 280.0f, 180.0f);
}
- (void) stepperValueIschanged
{
text = [NSString stringWithFormat:@"Stepper2 Value = %2.2f", stepper2.value];
stepper2Label.text = text;
insideLabel.text = text;
}
- (void) alertViewAction1
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
[alert1 show];
}
- (void) alertViewAction2
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Stepper Input"
message:@"\n\n\n" // IMPORTANT 製造空白區
delegate:self // call
cancelButtonTitle:@"取消"
otherButtonTitles:@"選擇", nil];
insideLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 250, 20)];
//資源分配與位置設定
stepper2 = [[UIStepper alloc] init];
// size value are ignore,注意大小不能設定,因此設為任何值都可以,此處為0
[stepper2 setFrame:CGRectMake(80,60 , 0, 0)];
//設定stepper的範圍與起始值
[stepper2 setMaximumValue:10.0];
[stepper2 setMinimumValue:0.0];
[stepper2 setValue:5.5];
//設定stepper每次增減的值
[stepper2 setStepValue:0.1];
//設定stepper可以按住不放來連續更改數值
[stepper2 setContinuous:YES];
//設定stepper是否循環(到最大值時再增加數值最從最小值開始)
[stepper2 setWraps:YES];
//將stepper加入UIControlEventValueChanged的觸發事件中並設定觸發時所處理的函式
[stepper2 addTarget:self action:@selector(stepperValueIschanged) forControlEvents:UIControlEventValueChanged];
[alert2 addSubview:stepper2];
[alert2 addSubview:insideLabel];
// 向上移動110點,平移設定
[alert2 setTransform:CGAffineTransformMakeTranslation(0.0, 110.0)];
//alertNumber = 1;
[alert2 show];
}
@end
4. 執行結果



沒有留言:
張貼留言