i have a problem like this:
i want to show a customized view inside a alert view. so i create a separate xib file and designed my interface.and implemented the class for it too.but when i apply below code,it gives me an error.
this is the code :
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Confirm your Action"
message:#"Click OK to confirm"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:#"Cancel",nil];
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"customDialogViewController" owner:self options:nil];
customDialogViewController *myView = (customDialogViewController*) [subviewArray objectAtIndex:0];
[alert setValue:myView forKey:#"accessoryView"];
//alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag = KAlertViewthree;
[alert show];
and this is my error :
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<ScheduleView: 0x8adf1a0> should have parent view controller:<_UIModalItemAccessoryViewControllerForLegacyAlert: 0xa888b70> but actual parent is:<UINavigationController: 0x8add8c0>'
i really new to iOS development.did i do this wrong ? where is the mistake ? i don't know what is this Nib file talking here "loadNibNamed:#bla boa " i just gave my xib name for that. can anyone give me a better way to do this or can you tell me the where i have to change to fix this issue ?
please guide me some one..
thank you.
This API has solution for your problem use this and have fun
https://github.com/Darktt/DTAlertView
https://github.com/Scott90/SDCAlertView
https://github.com/lmcd/LMAlertView
for all who having this problem,
i have followed this link..and for the star marks, i used this..
all you have to do is add below files to the project
CustomIOS7AlertView.h
CustomIOS7AlertView.m
JSFavStarControl.h
JSFavStarControl.m
and put below code where you want to pop up the alert view
// Here we need to pass a full frame
CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] init];
// Add some custom content to the alert view
[alertView setContainerView:[self createDemoView]];
// Modify the parameters
[alertView setButtonTitles:[NSMutableArray arrayWithObjects:#"Close1", #"Close2", #"Close3", nil]];
[alertView setDelegate:self];
// You may use a Block, rather than a delegate.
[alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
NSLog(#"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
[alertView close];
}];
[alertView setUseMotionEffects:true];
// And launch the dialog
[alertView show];
and inside the method of createDemoView,you have to implement your customised view.in my case it is like this
UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 100)];
//alertView.tag=2;
UILabel *rateLbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 290, 45)];
rateLbl.backgroundColor=[UIColor clearColor];
rateLbl.font=[UIFont boldSystemFontOfSize:15];
rateLbl.text=#"Rate";
UILabel *cmntLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 45, 290, 45)];
cmntLable.backgroundColor=[UIColor clearColor];
cmntLable.font=[UIFont boldSystemFontOfSize:15];
cmntLable.text=#"Add Comment";
UIImage *dot, *star;
dot = [UIImage imageNamed:#"dot.png"];
star = [UIImage imageNamed:#"star.png"];
JSFavStarControl *rating = [[JSFavStarControl alloc] initWithLocation:CGPointMake(150, 20) dotImage:dot starImage:star];
[rating addTarget:self action:#selector(updateRating:) forControlEvents:UIControlEventValueChanged];
UILabel *lblAlertTItle=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 290, 45)];
lblAlertTItle.backgroundColor=[UIColor clearColor];
lblAlertTItle.textAlignment=UITextAlignmentCenter;
lblAlertTItle.font=[UIFont boldSystemFontOfSize:18];
lblAlertTItle.text=#"Choose your sharing option";
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(150, 57, 100, 25)];
text.backgroundColor=[UIColor whiteColor];
//[demoView addSubview:lblAlertTItle];
[demoView addSubview:text];
[demoView addSubview:rating];
[demoView addSubview:rateLbl];
[demoView addSubview:cmntLable];
return demoView;
so my output is like this.
use it and have fun :)
thank you for everyone who helped me.
In to native Alert-view in ios7 that not possible to adding custom View as a sub-view of alert-view. You can use ios-custom-alertview for doing this kind of task you can do what you want. Hope this helpful to you.
I am using this ios-custom-alertview with Adding two Bellow file in to my project.
CustomIOS7AlertView.h
CustomIOS7AlertView.m
and how to use there is quick-start-guide
Related
I had develop a instant meaning checker like one in UIMENU Button,it working fine but since i had used the alert method to show the meaning,the appearnce of the alert is not good.can any one tell me how to get the appearance of that of the define method in UIMENU button
UIAlertView *av = [[UIAlertView alloc] initWithFrame:CGRectMake(0,0,300,200)];//[[UIAlertView alloc] initWithTitle:newstring message:nil delegate:nil cancelButtonTitle:#"ཕྱིར་འཐེན།" otherButtonTitles:nil];
av.title = newstring;
av.layer.backgroundColor=(__bridge CGColorRef)([UIColor colorWithRed:22/255.0f green:113/225.0f blue:9/255.0f alpha:1.0f]);
av.message = nil;
av.delegate = nil ;
[av addButtonWithTitle:#"ཕྱིར་འཐེན།"];
//[av addButtonWithTitle:#"OK"];
UITextView *v = [[UITextView alloc] initWithFrame:CGRectMake(0,0,300,200)]; //av.bounds];
v.text=newstring1;
[v setEditable:NO];
v.font=[UIFont fontWithName:#"Monlam Uni OuChan2" size:19];
[av setValue:v forKey:#"accessoryView"];
v.backgroundColor = [UIColor yellowColor];
[av show];
This is the code and the alert view that i got is
I'm assuming that you are using tap gesture to show the menu.
The following code displays a UIMenu of 100x100 with title as "Custom Action"
- (void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {
NSLog(#"tapGesture:");
// CGRect targetRectangle = self.tapView.frame;
CGRect targetRectangle = CGRectMake(100, 100, 100, 100);
[[UIMenuController sharedMenuController] setTargetRect:targetRectangle
inView:self.view];
UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:#"Custom Action"
action:#selector(customAction:)];
[[UIMenuController sharedMenuController]
setMenuItems:#[menuItem]];
[[UIMenuController sharedMenuController]
setMenuVisible:YES animated:YES];
}
For a detailed version, have a look here.
I'm trying to draw a rect on a UIView when a button is clicked, but for some reason isn't working...
- (IBAction)createNewGame:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
//create the new game and add to the array of games, save the index of actual game.
FIALGameModel *newGame = [[FIALGameModel alloc] initWithRows:_row columns:_column];
[_games addObject:(newGame)];
_actualGame = [_games count]-1;
if(_debug==true) {
NSString* messageString = [NSString stringWithFormat: #"Creating a New Game with %d rows and %d columns.", _row, _column];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Alert"
message: messageString
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 60, 60)];
test.backgroundColor = [UIColor redColor];
[_areaGame addSubview:test];
}
The alert works fine, but the rect don't.
As #matt hints in comment, the most usual cause of this is that you've not bound your properties or fields to your UI components properly, or else you've tried to run this code before the UIView was loaded. In either case, _areaGame is nil and the addSubview call silently does nothing. Setting a breakpoint on the addSubview line and inspecting _areaGame is the quickest way to verify.
I want to add a UISlider on a UIAlerView but I am not able to do that. I also google that but I found only two answers and both of them are not showing any any result here is code what I found and applied in my project:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"title" message:#"msg" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"ok", nil];
alertView.title = #"title";
CGRect frame = CGRectMake(alertView.frame.origin.x+10,alertView.frame.origin.y+20,100, 100.0);
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[slider setBackgroundColor:[UIColor clearColor]];
slider.minimumValue = 0.0;
slider.maximumValue = 10.0;
slider.continuous = YES;
slider.value = 25.0;
[alertView addSubview:slider];
[alertView show];
I think the frame of slider is not proper thats why its not being displayed.
can anyone help me in that.....
Thank you in advance...
You are adding the Slider in Alert view not in parent view of Alert view.
So your frame value will add the slider some where wide in alert view, result will be hidden on alert view.
so replace this line
CGRect frame = CGRectMake(alertView.frame.origin.x+10,alertView.frame.origin.y+20,100, 100.0);
To
CGRect frame = CGRectMake(10,20,100, 100.0);
The UIAlertView is also a UIView. I indeed, you could add the subview in it. But Apple says
"The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified."
So Better to have custom view which will act as alertview.Like this one
I use the following code to show a UIAlertView with a text field:
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* textField = [alertView textFieldAtIndex:0];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[alertView show];
The result looks like this on iOS 5.1:
and on iOS 6.1:
As you can see, the clear button is a bit higher than it should be on iOS 6.1. Any idea how to properly center the button? Adding textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; does not help either.
(Apparently, this is a bug in iOS 6.1 but maybe someone knows a workaround. Besides, the height of the text field in alert views also seem to differ in different OS versions but that is another issue.)
This looks like a bug from Apple, but you can always add your own clear button in the textField, doing something like this :
First create a UITextField property:
#property (nonatomic, strong) UITextField *textField;
And then assign it to the alertView's textField
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Rename", nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
self.textField = [alertView textFieldAtIndex:0];
self.textField.delegate = self;
//Create a custom clear button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 19, 19)];
[button setImage:[UIImage imageNamed:#"clearButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(clearText) forControlEvents:UIControlEventAllTouchEvents];
[self.textField setRightView:button];
self.textField.rightViewMode = UITextFieldViewModeAlways;
And then in the clearText method,
- (void)clearText {
[self.textField setText:#""];
}
I tried this out and it works
Hope this helps
Another way to do this is by overriding UITextField's clearButtonRectForBounds:.
I know that there has been one question about this but it is not clear to me. I want to add a UISlider to UIAlertView. In the UIAlertView, I also add one UILabel to show the value of UISlider. What I have been done so far is:
UIAlertView *alert= [[UIAlertView alloc]
initWithTitle: #"Set delay"
message: #" "
delegate: nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
UISlider *mySlider=[[UISlider alloc] initWithFrame:CGRectMake(20, 50, 200, 20)];
mySlider.maximumValue=10.00;
mySlider.minimumValue=1.00;
UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(220, 50, 50, 40)];
int x=.........;//x should get value of mySlider
[myLabel setText:[NSString stringWithFormat:#"%d",x]];
[alert addSubview:myLabel];
[alert addSubview:mySlider];
[alert show];
[alert release];
so I want variable x to get value of mySlider and the myLabel will show the value of x. I get stuck at this point. Any suggestion is really appreciated.
Here what you need to do:
Make the UILabel global, that mean declare it on the header file (this is to reach the label easily).
Then add a target to the slider, with ValueChanged event.
[mySlider addTarget:self action:#selector(sliderHandler:) forControlEvents:UIControlEventValueChanged];
next, implement the sliderHandler method:
- (void) sliderHandler: (UISlider *)sender {
int x = sender.value;
[myLabel setText:[NSString stringWithFormat:#"value = %d",x]];
}
That will work with you.