ActionSheet (AlertController) example...
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(120, 150, 60, 40);
btn.backgroundColor = [UIColor redColor];
btn.layer.borderWidth = 2;
btn.layer.borderColor = [UIColor greenColor].CGColor;
[btn setTitle:@"Press" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
btn.layer.cornerRadius = 10;
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)btnAction {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Directions"
message:@"Select mode of transportation"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *drivingAction = [UIAlertAction actionWithTitle:@"Driving" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// this block runs when the driving option is selected
NSLog(@"Clicked on Driving");
}];
UIAlertAction *walkingAction = [UIAlertAction actionWithTitle:@"Walking" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// this block runs when the walking option is selected
NSLog(@"Clicked on Walking");
}];
UIAlertAction *flyingAction = [UIAlertAction actionWithTitle:@"Flying" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// this block runs when the walking option is selected
NSLog(@"Clicked on Flying");
}];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:drivingAction];
[alert addAction:walkingAction];
[alert addAction:flyingAction];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
No comments:
Post a Comment