ActionSheet (Popover) With Dynamic Buttons
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Please Select data" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped.
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
//********************** Button With Image
UIAlertAction* online = [UIAlertAction
actionWithTitle:@"Add Another Data"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
}];
[online setValue:[[UIImage imageNamed:@"Add_Another.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];
[actionSheet addAction:online];
for (NSString *title in SelectDataArray) {
if ([title isEqualToString: strSelectedData])
{
//********************** Selected Data Highlight
[actionSheet addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[btnSelectData setTitle:title forState:UIControlStateNormal] ;
strSelectedWattage = title ;
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
}
else
{
[actionSheet addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[btnSelectData setTitle:title forState:UIControlStateNormal] ;
strSelectedData = title ;
[self dismissViewControllerAnimated:YES completion:^{
}];
}]];
}
}
//********************** For Ipad (popover)
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
[actionSheet setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [actionSheet
popoverPresentationController];
popPresenter.sourceView = btnSelectData;
popPresenter.sourceRect = btnSelectData.bounds;
}
// Present action sheet.
[self presentViewController:actionSheet animated:YES completion:nil];
*** ActionSheet With Diffrent TextColor and CheckMark
UIAlertController *customActionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//*********************************************************
//*********************************************************
UIAlertAction *Button = [UIAlertAction actionWithTitle:@"Buttons" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//click action
}];
[Button setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[Button setValue:[UIColor greenColor] forKey:@"imageTintColor"];
[Button setValue:@true forKey:@"checked"];
//*********************************************************
//*********************************************************
UIAlertAction *firstButton = [UIAlertAction actionWithTitle:@"First Button" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//click action
}];
[firstButton setValue:[UIColor blackColor] forKey:@"titleTextColor"];
[firstButton setValue:[UIColor blackColor] forKey:@"imageTintColor"];
[firstButton setValue:@true forKey:@"checked"];
//*********************************************************
//*********************************************************
UIAlertAction *secondButton = [UIAlertAction actionWithTitle:@"Second Button" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
//click action
}];
[secondButton setValue:[UIColor redColor] forKey:@"titleTextColor"];
//*********************************************************
//*********************************************************
UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
//cancel
}];
[cancelButton setValue:[UIColor yellowColor] forKey:@"titleTextColor"];
//*********************************************************
//*********************************************************
[customActionSheet addAction:firstButton];
[customActionSheet addAction:secondButton];
[customActionSheet addAction:cancelButton];
[customActionSheet addAction:Button];
[self presentViewController:customActionSheet animated:YES completion:nil];
No comments:
Post a Comment