Multiple UIAlertView; each with their own buttons and actions - xcode

Im creating a view in Xcode 4.3 and im unsure how to specify multiple UIAlertView's that have their own buttons with separate actions. Currently, my alerts have their own buttons, but the same actions. Below is my code.
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.examplesite1.com"]];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"examplesite2.com"]];
}
}
Thanks for any help!

There is a useful property tag for UIView(which UIAlertView subclass from). You can set different tag for each alert view.
UPDATE:
#define TAG_DEV 1
#define TAG_DONATE 2
- (IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
alert.tag = TAG_DEV;
[alert show];
}
- (IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
alert.tag = TAG_DONATE;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE){ // handle the donate
}
}

easier & newer
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
// first alert...
} else {
// sec alert...
}
}
all done!

If you find it dificult to use delegate methods to differently identifying alert view Then you can also use This Category class to use completion Block for each AlertView.
Alert_ActionSheetWithBlocks
For eg.
UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:#"AlertView+Block 1" message:#"WithBlocks" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }];
UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:#"AlertView+Block 2" message:#"WithBlocks" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];
I hope this one is the more easiest way as compare to tag + delegate method..

He's right but you need to add this:
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate
}
}
if buttonIndex==1 then you're using the FIRST otherbutton. 0 would be for cancel. But just do nothing for 0

Or you could do this (check the title name), is just another option... Mind identically titled alerts though!
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleOneGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleTwoGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
if([[alertView title] isEqualToString:#"titleOneGoesHere"])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.examplesite1.com"]];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"examplesite2.com"]];
}
}

Related

How to close an alert after some time and repeat it every 10 min

In my MAC OSX application. I am throwing a an alert pop up asking user to select yes or no. if user doesnt click any of the choices and may drag it to some corner. So i wanted to autoclose it after some time and again show the same alert. so i can ensure him to take same action.
Alert code i am using is
-(bool)VpnStatusUnableToConnect:(NSString *)alertMessage
{
if (nil != alertMessage) {
NSImage *alertIcon = [NSImage imageNamed:#"dock-alert"]; //my custom image placed in support files
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:#"Try Again"];
[alert addButtonWithTitle:#"Cancel"];
[alert setMessageText:alertMessage];
[alert setAlertStyle:NSWarningAlertStyle];
[alert setIcon:alertIcon];
[[alert window] setTitle:#"VPN Connection Status"];
[[alert window] setBackgroundColor: NSColor.whiteColor];
if ( [alert runModal] == NSAlertFirstButtonReturn)
{
return 1;
}
else
return 0;
}
return 0;
}
Modify your code as below and give a try
-(void)yourAlert{
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: #"OK"];
[alert setMessageText: #"Attention!!! This a critical Alert."];
[alert setAlertStyle: NSInformationalAlertStyle];
NSTimer *myTimer = [NSTimer timerWithTimeInterval:3
target:self
selector: #selector(killWindow:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSModalPanelRunLoopMode];
int choice = 0;
choice = [alert runModal];
if(choice != 0)
[myTimer invalidate];
}
-(void) killWindow:(NSAlert *)alert with:(NSTimer *) theTimer;
{
NSLog(#"killWindow");
[[alert window] abortModal];
}

Alertview for textfiled works on ios7 but not working on ios6.1

if ([district.text isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Please fill the fields" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if([myTextField.text isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Please fill the fields" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if ([catname.text isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Please fill the fields" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else if([msg.text isEqualToString:#""])
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Please fill the fields" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
This is my code it works perfectly in ios7 but not in ios6. what changes should I make to work it on ios 6.1
You can't easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it.
One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. You can access it in the UIAlertView delegate callback by using UITextField *textField = [alertView textFieldAtIndex:0];.
Example:
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Alert"
message:#"enter your details:"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
/* Display a numerical keypad for this text field */
UITextField *textField = [alertView textFieldAtIndex:0];
textField.keyboardType = UIKeyboardTypeNumberPad;
[alertView show];

Send SMS in iOS 7 it not work

- (IBAction)button_number1:(id)sender {
MFMessageComposeViewController *textComposer = [[MFMessageComposeViewController alloc] init];
[textComposer setMessageComposeDelegate:self];
if ([MFMessageComposeViewController canSendText]) {
[textComposer setRecipients:#[string_numberphone]];
[textComposer setBody:#"ABC"];
[self presentViewController:textComposer animated:YES completion:NULL];
} else {
NSLog(#"Can't Open Text");
}
}
}
when I click a button_number1, it does not work, and no arlet Error.
But when clicking the button below, it works normally on iOS 7 but I can not assign the message content.
Please guide me how to assign the message content to this button_number2.
- (IBAction)button_number2:(id)sender {
NSString *message = [[NSString alloc] initWithFormat:#"sms:%#",string_numberphone];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:message ]];
}
Thank so much!
in .h header file.....
#import <MessageUI/MessageUI.h>
#interface MainViewController : UIViewController <MFMessageComposeViewControllerDelegate>{
- (IBAction)sendsms:(id)sender;
}
in .m implementation file.....
- (IBAction)showEmail:(id)sender{
if (phoneNumber!=NULL) {
UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:#"iPhone"] ) {
NSString *phNo = [phoneNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSURL *phoneUrl = [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",phNo]];
if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
[[UIApplication sharedApplication] openURL:phoneUrl];
} else
{
UIAlertView *calert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Call facility is not available!!!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[calert show];
[calert release];
}
} else {
UIAlertView *warning =[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Your device doesn't support this feature." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warning show];
[warning release];
}
}
else {
UIAlertView *warning =[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Phone No is not Available." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warning show];
[warning release];
}
}

how can i update a blob data from a image in sqlight in xcode

how can i update image in sqlite
I have an image from imagePickerView and i want to save it in blob format in my sql how can that posibale my sorce code hear
NSString *item = str_Name;
sqlite3_stmt *statement;
NSString *databasePath = [[NSBundle mainBundle] pathForResource:#"SLAMEBOOK" ofType:#"sql"];
if(sqlite3_open([databasePath UTF8String], &SLAMEBOOK) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"UPDATE SlammyDetail SET Name= \"%#\", BDate=\"%#\" WHERE Name = \"%#\"", txt_Name.text ,lbl_date.text, item];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(SLAMEBOOK, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"REMINDER Updated"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Failed to add Task"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
sqlite3_finalize(statement);
sqlite3_close(SLAMEBOOK);
}
[txt_Name resignFirstResponder];
[[UIApplication sharedApplication] cancelAllLocalNotifications];

send file using bump

I have implemented correctly bump's api, and added this code:
- (void) configureBump {
[BumpClient configureWithAPIKey:#"your api key" andUserID:[[UIDevice currentDevice] name]];
[[BumpClient sharedClient] setMatchBlock:^(BumpChannelID channel) {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Matched with user" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Matched with user: %#", [[BumpClient sharedClient] userIDForChannel:channel]);
[[BumpClient sharedClient] confirmMatch:YES onChannel:channel];
}];
[[BumpClient sharedClient] setChannelConfirmedBlock:^(BumpChannelID channel) {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Channel with" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Channel with %# confirmed.", [[BumpClient sharedClient] userIDForChannel:channel]);
[[BumpClient sharedClient] sendData:[[NSString stringWithFormat:#"hi"] dataUsingEncoding:NSUTF8StringEncoding]
toChannel:channel];
}];
[[BumpClient sharedClient] setDataReceivedBlock:^(BumpChannelID channel, NSData *data) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Data received" message:[NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];
NSLog(#"Data received from %#: %#",
[[BumpClient sharedClient] userIDForChannel:channel],
[NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding]);
NSString *receivedBumpData=[NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding];
if(receivedBumpData.length!=0){
CardAvailableLandscape *cardObject=[[CardAvailableLandscape alloc] init];
[cardObject bumpInsertFunction:receivedBumpData];
}
}];
[[BumpClient sharedClient] setConnectionStateChangedBlock:^(BOOL connected) {
if (connected) {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"bump Coneected" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Bump connected...");
} else {
/* UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Bump disconnected..." message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Bump disconnected...");
}
}];
[[BumpClient sharedClient] setBumpEventBlock:^(bump_event event) {
switch(event) {
case BUMP_EVENT_BUMP:{
/*UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Bump detected." message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"Bump detected.");
break;
}
case BUMP_EVENT_NO_MATCH:
{
/*UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"No match." message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil ];
[alert show];
[alert release];*/
NSLog(#"No match.");
break;
}
}
}];
}
this code has been taken from the example project. the connection is established. but i can't seem to be able to send a file: I tried this tutorial:
http://appgenor.blogspot.it/2010/02/using-bumps-new-api-to-exchange-data.html
but the Bumb object can not be created. it gives me an error. the bump has not been implemented. so it is not in the sdk, i think...
bumpObject = [[Bump alloc] init]; // Bump *bumpObject;
help please!!
I looked at the link you mention and it seems a bit old. You create bump object using static function: [BumpClient sharedClient], which also calls connect for you. I am trying to play with it as well but having problems where bump is detected but I can send data. I am using an iPod and bu.mp website to test the code.

Resources