adding a custom view to a alert view
Asked Answered
C

3

7

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.

Cleareyed answered 25/4, 2014 at 6:32 Comment(4)
,Yesterday I have already told you in my answer that its not a problem of UIAlertView.In your console log its clearly mentioned that there is some issue in your viewcontroller.So change your question .Its not a problem of UIAlertView.Cida
sir , i have followed what you have said yesterday. but didn't worked. so i follow this github.com/wimagguc/ios-custom-alertview. somehow i able to add star marks to the alert custom view. but can not add another text fields and lablals can you suggest me a wayCleareyed
I have created it on github.DOwnload this and let me know regarding your issue on UIAlertView. github.com/jayaprada-behera/CustomAlertView . But The above issue you mentioned is not related to UIAlertViewCida
One way is to design your own view in a View Controller and then load it modally. See this answer.Bales
C
3

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.

enter image description here

Cleareyed answered 28/4, 2014 at 9:40 Comment(0)
I
3

enter image description here

This API has solution for your problem use this and have fun

https://github.com/Darktt/DTAlertView

https://github.com/Scott90/SDCAlertView

enter image description here

https://github.com/lmcd/LMAlertView

Inject answered 25/4, 2014 at 6:50 Comment(0)
C
3

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.

enter image description here

Cleareyed answered 28/4, 2014 at 9:40 Comment(0)
A
2

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

Apocynthion answered 25/4, 2014 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.