In my iOS 8 application I am using UIActionSheet. I tried to change the color of the button title in willPresentActionSheet delegate method, but it is not recognizing buttons as its subview.
I constructed UIActionSheet like this:
UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:#"Select an Option" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil];
popup.tag = 2;
[popup addButtonWithTitle:#"Add Tag To Chat"];
[popup addButtonWithTitle:#"Remove Tag From Chat"];
[popup addButtonWithTitle:#"Terminate"];
[popup addButtonWithTitle:#"Cancel"];
[popup showInView:self.view];
Delegate is:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
NSLog(#"%d",[actionSheet.subviews count]);
for (UIView *subview in actionSheet.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)subview;
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
Buttons are showing, I can click and action performs. But it says count = 0. Why?
Try this way, changing the UIColor of subviews:
UIActionSheet * action = [[UIActionSheet alloc]
initWithTitle:#"Title"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"",nil];
[[[action valueForKey:#"_buttons"] objectAtIndex:0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
I've tried it. It is working...
The delegate methods in which you're trying to check the number of subviews: willPresentActionSheet seems inappropriate.
willPresentActionSheet is called before the action sheet actually gets rendered.
If you check the number of subviews in didlPresentActionSheet, that will be more appropriate.
Hope this helps..
Related
I want to add checkbox exactly as the below image
below is my code:
UIButton *nameField = [[UIButton alloc] initWithFrame:CGRectMake(120, 0, 30, 30.0)];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 50)];
[v setBackgroundColor:[UIColor clearColor]];
[nameField setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[v addSubview:nameField];
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Delete selected pods from server?" message:
#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[av setValue:v forKey:#"accessoryView"];
av.message = #"This will delete all the posts related to the pictures you want to delete!";
[av show];
with the above code I am getting as :
I want to get checkbox beside message..
Any help or suggestion how to proceed?
You can use https://github.com/wimagguc/ios-custom-alertview
Create your custom content view and set it like
CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
UIView *customView ..; //Your custom view
[alertView setContainerView:customView];
I would suggest you to do not do that.
accessoryView is not a public exposed API and you risk an app rejection
UIAlertView is deprecated in iOS8, use UIAlertController
You can use a third party library, I usually integrate MRProgress in my projects, you can create your own nib and add it.
I have the below code to draw a datepicker (showing around the middle of the screen). It works in iOS 7 but in iOS 8 it is not showing. Only the "Select Expiry Date" is shown in a small box in the bottom of the screen.
menu = [[UIActionSheet alloc] initWithTitle:#"Select Expiry Date"
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
pickerView = [[CDatePickerViewEx alloc] init];
CGRect pickerRect = pickerView.bounds;
pickerRect.origin.y = 50;
pickerRect.size.height = 100;
pickerView.frame = pickerRect;
UIButton *startbtn = [UIButton buttonWithType:UIButtonTypeCustom];
startbtn.frame = CGRectMake(240,12, 60, 25);
[startbtn setTitle:#"OK" forState:UIControlStateNormal];
[startbtn setBackgroundImage:[UIImage imageNamed:#"btn"] forState:UIControlStateNormal];
[startbtn addTarget:self action:#selector(pressedbuttonCancel:) forControlEvents:UIControlEventTouchUpInside];
[menu addSubview:pickerView];
[menu addSubview:startbtn];
[menu showInView:self];
[menu setFrame:CGRectMake(0,200,320, 260)];
Check if AutoLayout is enabled for your view, as well as Size classes. I ran into a similar issue before and remember of fixing it with programmatic constraints.
When i click the button, pickerview coming up and i am choosing a city from there. And if i click again i want to get what i selected before.
Here is my code,
-(void)cityButtonPressed{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 30, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
NSLog(#"selectedPickerView = %i",selectedPickerView);
[pickerView selectRow:selectedPickerView inComponent:0 animated:NO];// this line is not working
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
NSString *done = #"Done";
closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:done]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 10, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor greenColor];
[closeButton addTarget:self action:#selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 500)];
[pickerView reloadAllComponents];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
// Handle the selection
[cityButton setTitle:[cities objectAtIndex:row] forState:UIControlStateNormal];
if (row == 0) {
[cityButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}else{
[cityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
selectedPickerView = row;
}
What am i doing wrong here.
And one more question. When i click the button,this error is coming.
<Error>: CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
I am wondering, is it not working because of this error?
You're creating an action sheet with no title or buttons? Ugh. Then you're adding a segmented control as a tappable button? Double ugh Apple will likely reject an app that uses a segmented control as a button.
As to your specific problem, I'm not sure if you can change the selected component in a picker view before it's on-screen. What happens if you move your selectRow:inComponent:animated: call down to after displaying the action sheet? You should probably also get rid of the reload call on your picker view, since it will load it's data when it's displayed.
The CANCEL area is over the top of the date, the picker still works but is hidden under the cancel button.
This code used to work so I am guessing its something with the newer OS?
I haven't needed to mess with the iPhone code for a while and now find a problem with a date picker. So I am not up to speed at all on XCode but trying to muddle through to see what the problem is. I guess I will eventually get up to speed again and figure out what is wrong, but if anyone can tell me what it is that would be awesome.
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {
switch ([actionSheet tag] ) {
case 1://date
{
NSDate *d;
UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, 100, 116)];
[pickerView setTag:100+[actionSheet tag]];
[pickerView setDatePickerMode:UIDatePickerModeDate];
[actionSheet addSubview:pickerView];
[pickerView release];
NSArray *subViews = [actionSheet subviews];
[[subViews objectAtIndex: SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subViews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}
break;
}
}
- (IBAction)btnDate:(id)sender {
UIActionSheet *asheet = [[UIActionSheet alloc]
initWithTitle:#"Pick the date"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Select"
, nil];
[asheet setTag:1];
[asheet showInView:[self.view superview]];
[asheet setFrame:CGRectMake ( 0, 117, 320, 383)];
[asheet release];
}
I've added an UIPickerView into a UIActionSheet. Everything ok, but my picker can't be scrolled to choose a value. Any suggestions?
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIPickerView * picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 320, 270)];
picker.showsSelectionIndicator = YES;
picker.dataSource = self;
picker.delegate = self;
[actionSheet addSubview:picker];
[picker release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 500)];
[actionSheet release];
Best regards,
Dorin
An alternative solution to what you're trying to achieve is to set text field's inputView and inputAccessoryView. Setting these will replace the standard keyboard and bring in custom views. This includes the picker view. I happened to write a short example in this question here. Take a look and see if it helps.