I have used this to implement my custom alert view. To my alert view , I have included a UITextfield to input some details. My problem is how I get the input text from the alert view when the button is pressed.
My implementation is like thisL
- (UIView *)createDemoView
{
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=NSTextAlignmentCenter;
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;
}
-(void)buttonTappedDone:(cellForDatePickCell*)cell{
NSString* appoinmentID =[NSString stringWithFormat:#"%#",cell.appoinment_Dtepick];
NSString* userID = [NSString stringWithFormat:#"%#",cell.USER_Dtepick];
NSDictionary* paras = [[NSDictionary alloc]initWithObjectsAndKeys:appoinmentID,#"appointmentId",userID,#"userId", nil];
jsonpaser* jpser = [[jsonpaser alloc]init];
//[self.indicator_process startAnimating];
[jpser getWebServiceResponce:#"MYYURL" :paras success:^(NSDictionary *responseObject)
{
//requestsF_date = responseObject;
NSLog(#"Appoinment Completed :%#",responseObject);
NSString* selecteDate = [ScheduleView getDate];
NSString* prsonID =[LoginView getPersonID];
NSDictionary* parms = [NSDictionary dictionaryWithObjectsAndKeys:prsonID,#"caregiverPersonId",selecteDate,#"selectedDate", nil];
jsonpaser* jp = [[jsonpaser alloc]init];
[jp getWebServiceResponce:#"MyUrl" :parms success:^(NSDictionary *responseObject)
{
requestsF_date = responseObject;
NSLog(#"Done Clicked :%#",requestsF_date);
[self.tableView reloadData];
}];
}];
// 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:#"OK", #"Cancel", 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 %ld.", buttonIndex, (long)[alertView tag]);
if (buttonIndex==0) {
NSLog(#"button zero [ OK] clicked");
// here i want to get the text box value
}
if (buttonIndex==1) {
NSLog(#"button 1 [ Cancel] clicked");
}
[alertView close];
}];
[alertView setUseMotionEffects:true];
// And launch the dialog
[alertView show];
}
This alert view is popes up when a button clicked in a table view row. can anyone tell me how can i take the textfield value here ?
thank you
The solution to your problem is as simple as a global declaration of the UITextField that you want to access.
Declare the UITextField globally.
Alloc init the text field and add it to the AlertView in your createDemoView method.
In the buttonTappedDone method, check for the text in the UITextField and if not null, you can use it from there.
Related
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 created a method called ellipses in UIView and this method is creating an actionsheet.I tried to call this method in my UIViewController but it failed because of "[actionSheet showInView:self]".This actionsheet works fine in my view because of 'self' but I dont know how to fix the 'self' so that it can show both in my VC and my View.
here is part of my code in UIView
- (void)ellipses{
NSString *deleteButtonName = NSLocalizedString(#"Delete", #"");
NSString *copyButtonName = NSLocalizedString(#"Copy to clipboard", #"");
NSString *cancel = NSLocalizedString(#"Cancel", #"");
NSArray *buttons;
buttons = [NSArray arrayWithObjects:deleteButtonName, copyButtonName, nil];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
for(NSString *title in buttons) {
[actionSheet addButtonWithTitle:title];
}
[actionSheet addButtonWithTitle:cancel];
[actionSheet setCancelButtonIndex:[buttons count]];
[actionSheet showInView:self]; // here is gonna be a problem later
}
here is part of code in my UIViewController
-(void)loadView{
newView *nView = [newView new];
[self setView:nView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton setFrame:CGRectMake(200, 230, 60, 28)];
[testButton setUserInteractionEnabled:YES];
[testButton addTarget:self action:#selector(actionSh) forControlEvents:UIControlEventTouchUpInside];
[nView addSubview:testButton];
}
-(void)actionSh{
newView *newview = [[newView alloc] init];
[newview ellipses];//CALL ellipses
[newview setNeedsDisplay];
}
The Answer for my question:
add this method in my VC.newView is my UIView.
- (newView *) newview {
return (newView *)[self view];
}
and call methods from my VC
-(void)actionSH {
[[self newview] ellipses];
}
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
Im pretty new in programming and im currently looking at blocks which im having some issues with. Im having a hard time reaching an object which is defined within my block. Actually I cannot really explain this issue in an accurate way, so ill give you some code and pictures, and hope that it makes sense and that you excuse my dodgy knowledge and try to help me understand the problem.
So Im starting off by running this block
- (void)fetchSite
{
//Initiate the request...
[[FeedStore sharedStore] fetchSites:^(RSSChannel *objSite, NSError *err) {
//When the request completes, this block will be called
if (!err) {
//If everything went ok, grab the object
channelSite = objSite;
//Now we need to extract the first siteId and give it to sharedstore
RSSItem *site = [[channelSite sites] objectAtIndex:0];
NSString *currentSiteId = [NSString stringWithFormat:#"%#", [site siteNumber]];
NSLog(#"DEBUG: LVC: fetchSite: currentSiteId: %#", currentSiteId);
[[FeedStore sharedStore] setCurrentSiteId:currentSiteId];
//Now we can call fetchEntries
[self fetchEntries];
} else {
//If things went bad, show an alart view
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Fel" message:#"Kunde inte bedömma din position relaterat till närmaste hållplats, försök gärna igen" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[av show];
//Stop UIRefreshControl
[refreshControl endRefreshing];
}
}];
}
Then fetchEntries is called:
- (void)fetchEntries
{
void (^completionBlock)(RSSChannel *obj, NSError *err) = ^(RSSChannel *obj, NSError *err) {
if (!err) {
//If everything went ok, grab the object, and reload the table and end refreshControl
channel = obj;
//Post notification to refreshTableView method which will reload tableViews data.
[[NSNotificationCenter defaultCenter] postNotificationName:#"RefreshTableView" object:nil];
//Stop UIRefreshControl
[refreshControl endRefreshing];
} else {
//If things went bad, show an alart view
UIAlertView *av = [[UIAlertView alloc] initWithTitle:#"Fel" message:#"Kunde inte hämta avgångar för din hållplats, försök gärna igen" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[av show];
//Stop UIRefreshControl
[refreshControl endRefreshing];
}
};
//Initiate the request...
if (fetchType == ListViewControllerFetchBus) {
[[FeedStore sharedStore] fetchDepartures:completionBlock];
selectedSegment = 0;
} else {
[[FeedStore sharedStore] fetchDeparturesMetro:completionBlock];
selectedSegment = 1;
}
}
Reloading the table:
- (void)refreshTableView:(NSNotification *)notif
{
[[self tableView] reloadData];
}
Heres where I populate my custom table cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get received data from fetchEntries block
RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];
//Get the new or recycled cell
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
//Set the apperance
[[cell lineNumberLabel] setTextColor:[UIColor whiteColor]];
[[cell lineNumberLabel] minimumScaleFactor];
[[cell lineNumberLabel] setFont:[UIFont boldSystemFontOfSize:22]];
[[cell lineNumberLabel] adjustsFontSizeToFitWidth];
[[cell lineNumberLabel] setTextAlignment:NSTextAlignmentCenter];
[[cell destinationLabel] setTextColor:[UIColor whiteColor]];
[[cell displayTimeLabel] setTextColor:[UIColor whiteColor]];
[[cell displayTimeLabel] setTextAlignment:NSTextAlignmentLeft];
//Configure the cell with the item
if ([item lineNumber]) [[cell lineNumberLabel] setText:[item lineNumber]];
if ([item destination]) [[cell destinationLabel] setText:[item destination]];
if ([item displayTime]) [[cell displayTimeLabel] setText:[item displayTime]];
if ([item displayRow1]) [[cell destinationLabel] setText:[item displayRow1]];
return cell;
}
So when im running through these blocks in normal state everything works like a charm.
But the issues starts when Im calling fetchSite method from an UIActionSheet, or actually from an UISegmentedControl which is a subview of UIActionSheet, this takes place here:
- (void)displayPickerView
{
//First im creating an instance to PickerViewController which will handle the UIPickerView which is a subview of UIActionSheet. pickerArrayFromChannel holds entries for UIPickerView to show in its ViewController.
pickerViewController = [[PickerViewController alloc] init];
[pickerViewController initWithItems: pickerArrayFromChannel];
pickerViewController.delegate = self;
//initiate UIActionSheet which needs no delegate or appearance more than its style
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
//Create frame and initiate a UIPickerView with this. Set its delegate to the PickerViewController and set it to start at row 0
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.delegate = pickerViewController;
pickerView.dataSource = pickerViewController;
[pickerView selectRow:0 inComponent:0 animated:YES];
//Add this pickerView as subview to actionSheet
[actionSheet addSubview:pickerView];
//Now, create two buttons, closeButton and okButton. Which really are UISegmentedControls. Action to closeButton is (for now) dismissActionSheet which holds only following code " [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; " and works fine
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Stäng"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
//OK button
UISegmentedControl *okButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Visa avgångar"]];
okButton.momentary = YES;
okButton.frame = CGRectMake(10, 7.0f, 100.0f, 30.0f);
okButton.segmentedControlStyle = UISegmentedControlStyleBar;
okButton.tintColor = [UIColor blueColor];
[okButton addTarget:self action:#selector(updateDeparturesFromActionSheet) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:okButton];
//Now show actionSheet
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
Now lets look at updateDeparturesFromActionSheet method and we're getting close to where my issues begin (if your still reading, thank you for that
- (void)updateDeparturesFromActionSheet
{
//Dismiss the actionSheet
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
//Send the selected entry from UIPickerView to currentAddress property in FeedStore, fetchSite method will eventually use this
[[FeedStore sharedStore] setCurrentAddress:[pickerViewController addressFromPickerView]];
//Call fetchSite method
[self fetchSite];
}
fetchSite then eventually calls fetchEntries method, which eventually populate my table. I store received obj (from block) in an property called channel
void (^completionBlock)(RSSChannel *obj, NSError *err) = ^(RSSChannel *obj, NSError *err) {
if (!err) {
//If everything went ok, grab the object, and reload the table and end refreshControl
channel = obj;
The channel object looks like this
channel RSSChannel * 0x07464340
NSObject NSObject
currentString NSMutableString * 0x00000000
lvc ListViewController * 0x08c455d0
items NSMutableArray * 0x08c452d0
sites NSMutableArray * 0x08c452f0
parentParserDelegate id 0x00000000
pickerViewArray NSMutableArray * 0x08c45320
As you can see when I populate my tables cells, I use the info from items inside of the channel object.
Now if your still with me (and havnt fallen asleep) so far I will explain my actual problem, now when you have (hopefully) all the relevant code.
So when I press okButton in my UIActionSheet, updateDeparturesFromActionSheet method gets called. Which calls fetchSite which eventually calls fetchEntries and so far so good, these blocks performs and I get back information. But when I grab fetchEntries obj and put it in channel, it doesnt seems to "update" this object with the new information grabbed by the blocks obj object. Among others it does not seems that channel object gets a new place in the memory (it keeps 0x07464340).
My first thought was to make sure channel object gets released by ARC, by removing all owners of that object, but it seems that even if I do so (and then doublecheck that its null with simple NSLog(#"%#") it keeps getting its old values and memory reference back when I trigger the "update".
After many attempts to release the channel object and doing all sorts of stuff (creating new arrays (outside of block) amongst other things). I keep thinking that some special rule within blocks that I dont understand is messing with me. Any ideas?
Please let me know if anything isnt clear because I've expressed myself bad, its hard to explain an issue that you dont understand yourself. Thanks in advanced.
I took this project from github.
It is custom AlertView. I understood how it works. Modified it for my project, like this
Course1 - name of some product, and '1' - is its amount. When I'm typing on plus/minus amount increases/decreases. But it works only for my variable (which I load on this AlertView, when I show it, and it is 1). How can I reload message of this alert view with changing var (amount). I can't understand.
Here my code.
In my class I call alert view by this code
BlockAlertView *alert = [BlockAlertView alertWithTitle: title message:ac.acCount];
[alert setCancelButtonWithTitle:#"-" block:nil];
[alert setDestructiveButtonWithTitle:#"+" block:^{
int u = [ac.acCount intValue];
u++;
ac.acCount = [NSString stringWithFormat:#"%d", u];
NSLog(#"%d", u);
}];
[alert addButtonWithTitle:#"Ok" block:^{
NSMutableArray *container = [[NSMutableArray alloc] init];
[container addObject:title];
[container addObject:price];
[container addObject:bId];
[container addObject:ac.acCount];
[container addObject:depid];
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:container];
}];
[alert show];
Show method just draws alert view with params which takes from alertWithTitle: title message:ac.acCount. Here is the code
+ (BlockAlertView *)alertWithTitle:(NSString *)title message:(NSString *)message
{
return [[[BlockAlertView alloc] initWithTitle:title message:message] autorelease];
}
here is
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
NSLog(#"title - %# message - %#", title, message);
if ((self = [super init]))
{
UIWindow *parentView = [BlockBackground sharedInstance];
CGRect frame = parentView.bounds;
frame.origin.x = floorf((frame.size.width - background.size.width) * 0.5);
frame.size.width = background.size.width;
_view = [[UIView alloc] initWithFrame:frame];
_blocks = [[NSMutableArray alloc] init];
_height = kAlertViewBorder + 6;
if (title)
{
CGSize size = [title sizeWithFont:titleFont
constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000)
lineBreakMode:UILineBreakModeWordWrap];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)];
labelView.font = titleFont;
labelView.numberOfLines = 0;
labelView.lineBreakMode = UILineBreakModeWordWrap;
labelView.textColor = kAlertViewTitleTextColor;
labelView.backgroundColor = [UIColor clearColor];
labelView.textAlignment = UITextAlignmentCenter;
labelView.shadowColor = kAlertViewTitleShadowColor;
labelView.shadowOffset = kAlertViewTitleShadowOffset;
labelView.text = title;
[_view addSubview:labelView];
[labelView release];
_height += size.height + kAlertViewBorder;
}
if (message)
{
CGSize size = [message sizeWithFont:messageFont
constrainedToSize:CGSizeMake(frame.size.width-kAlertViewBorder*2, 1000)
lineBreakMode:UILineBreakModeWordWrap];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(kAlertViewBorder, _height, frame.size.width-kAlertViewBorder*2, size.height)];
labelView.font = messageFont;
labelView.numberOfLines = 0;
labelView.lineBreakMode = UILineBreakModeWordWrap;
labelView.textColor = kAlertViewMessageTextColor;
labelView.backgroundColor = [UIColor clearColor];
labelView.textAlignment = UITextAlignmentCenter;
labelView.shadowColor = kAlertViewMessageShadowColor;
labelView.shadowOffset = kAlertViewMessageShadowOffset;
labelView.text = message;
[_view addSubview:labelView];
[labelView release];
_height += size.height + kAlertViewBorder;
}
_vignetteBackground = NO;
}
return self;
}
I tried to use something like this
-(void)reloadAlertView: (NSString *) title: (NSString *) message{
[self initWithTitle:title message:message];
}
and call this from my class, where i shows alert view.
[alert reloadAlertView: title: newMessage];
YOu need to ref a new label in your CustomAlertView.h
#property (nonatomic, strong) IBOutlet UILabel *mylab;
then you can get the properties of this label by the object of the CustomAlertView.h class
BlockAlertView *alert ...;
alert.mylab.text = #"hello";
solution (simple , not required to hack the BlockAlertView.m)
whenever the + button is click, the alert will be disappeared, and reappeared again.
ViewController.m
#implementation ViewController {
BlockAlertView *alert ;
NSInteger value;
}
-(void) viewDidLoad {
value = 0;
[self reloadAlert];
}
-(void) reloadAlert {
value += 1;
__block ViewController* _self = self;
alert = [[BlockAlertView alloc] initWithTitle:#"Course1" message:[[[NSNumber alloc] initWithInteger:value ] stringValue]];
[alert setDestructiveButtonWithTitle:#"PLUS" block:^{
[_self reloadAlert];
}];
[alert show];
}
}
solution , if you want to keep the AlertView remained, without popping up.
a. Modify BlockAlertView.h to bring messageLabelView outside to be accessed from ViewController, so we can redraw the message later.
BlockAlertView.h
#interface BlockAlertView {
...
}
#property (nonatomic, retain) UILabel *messageLabelView;
You can also bring the UIView *_view out of #protected and into #property instead, if preferred. In that case, messageLabelView can be access via _view 's subviews
b. Modify BlockAlertView.m dismissWithClickedButtonIndex:animated: to disable removeView function from being called if buttonIndex == PLUS sign button - buttonIndex to be hardcoded to 0.
There may be a better option to stop the execution thread inside the block obj of PLUS button, but I dont know how to do :)
c. Modify BlockAlert.m initWithTitle:message: to add reference of messageLabelView into labelView during the render of message Label.
BlockAlertView.m
#synthesize messageLabelView;
- (id)initWithTitle:(NSString *)title message:(NSString *)message
{
...
// code to initialize messageLabelView
if (message)
{
....
// code to render labelView for message. add reference to messageLabelView
messageLabelView = labelView;
// [labelView release];
}
}
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
if (buttonIndex >= 0 && buttonIndex < [_blocks count])
{
id obj = [[_blocks objectAtIndex: buttonIndex] objectAtIndex:0];
if (![obj isEqual:[NSNull null]])
{
((void (^)())obj)();
}
}
// there may be a better option to stop the execution thread inside the block obj above
if(buttonIndex == 0) {// or whatever index of Plus Button, dont removeView
return;
}
// code to removeView below, keep as usual
if (animated)
{
...
}
else
{
....
}
}
}
d. Change reloadAlert in ViewController.m
ViewController.m
-(void) reloadAlert {
value += 1;
alert.messageLabelView.text = [[[NSNumber alloc] initWithInteger:value ] stringValue];
[alert.messageLabelView setNeedsDisplay];
}