Two UIAlertView - xcode6

I have two UIAlertview ,i want to go to different page when i click inside,i create two xib file,and import them in main Viewcontroller,but don't know why can't show it,that is nothing wrong flag when i check~
Here is my code:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"LP01;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:#"LP01;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
- (void)alert:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
AViewController *switch1 = [[AViewController alloc]
initWithNibName:nil bundle:nil];
[self presentViewController:switch1 animated:YES completion:NULL];
}
}
- (void)alert1:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
BViewController *switch2 = [[BViewController alloc]
initWithNibName:nil bundle:nil];
[self presentViewController:switch2 animated:YES completion:NULL];
}
}
Please Help and Thanks~

UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"LP01;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"LP01;" message:#"No Connection" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Help", nil];
// Remember Delegate methods will be same for any number of elements related to it. So whether you have 2 or 3 or 5 UIAlertView's you delegate method will be written only once.
- (void)alert:(UIAlertView *)alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertview == firstAlert){
AViewController *switch1 = [[AViewController alloc]
initWithNibName:nil bundle:nil];
[self presentViewController:switch1 animated:YES completion:NULL];
}
else if(alertview == secondAlert){
BViewController *switch2 = [[BViewController alloc]
initWithNibName:nil bundle:nil];
[self presentViewController:switch2 animated:YES completion:NULL];
}
}
// Don't forget to call [firstAlert show]; or [secondAlert show]; to show your alert

Related

Camera not working in landscape mode only project?

I have developed landscape mode only project and UIcamera picture controller using take photo but it crashed when camera launched and it will show this reason: Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:(NSString *) kUTTypeImage,nil];
imagePicker.allowsEditing = NO;
[self presentModalViewController:imagePicker animated:YES];
// [self presentViewController:imagePicker animated:NO completion:^{
// [imagePicker setShowsCameraControls:YES];
// }];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"No camera available" message: #"Failed to take image" delegate: self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Try this friends:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No camera available" message:#"Failed to take image" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}

MFMessageComposeViewController - Getting stuck after sending SMS in ios7

After sending SMS from my phone(it's working properly) in my project using the MFMessageComposeViewController I can't get back to the app or press cancel. Instead I'm stuck in Apple SMS interface and I can't do anything else then to restart the app. Do I miss something in didFinishWithResult or could it be something else? Thanks
- (IBAction)sendSMS:(id)sender {
MFMessageComposeViewController *controller =
[[[MFMessageComposeViewController alloc] init] autorelease];
if([MFMessageComposeViewController canSendText])
{
NSString *str= #"Hi";
controller.body = str;
controller.recipients = [NSArray arrayWithObjects:
#"", nil];
controller.delegate = self;
[self dismissViewControllerAnimated:YES
completion:nil];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult) result
{
switch (result) {
case MessageComposeResultCancelled:
break;
case MessageComposeResultFailed:
{
UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Failed to send SMS!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[warningAlert show];
break;
}
case MessageComposeResultSent:
break;
default:
break;
}
[self dismissViewControllerAnimated:YES completion:nil];
}

how to create any UIViewController from NSString name dynamically iOS7.1

I want to create a UIViewController from NSString Name (buttonSelected.viewControllerID), considering class name is the same than storyboardIdentifier how to do that???, currently I have following code for two viewControllers, but I only want to have it in one shot
if ([buttonSelected.typeOfContent isEqualToString:#"AtencionClienteDetalleVC"]) {
if ([[functions removeBlanksFromString:buttonSelected.viewControllerID] isEqualToString:#""]) {
[[[UIAlertView alloc] initWithTitle:#"Notification"
message:[NSString stringWithFormat:#"screen destination must not be empty"]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
else{
AtencionClienteDetalleVC *viewController = [self.storyboard instantiateViewControllerWithIdentifier:buttonSelected.viewControllerID];
viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
viewController.itemsCatalog = buttonSelected.viewControllerParameters;
[self.navigationController pushViewController:viewController animated:YES];
}
return;
}
if ([buttonSelected.typeOfContent isEqualToString:#"ListadoVideos"]) {
if ([[functions removeBlanksFromString:buttonSelected.viewControllerID] isEqualToString:#""]) {
[[[UIAlertView alloc] initWithTitle:#"Notification"
message:[NSString stringWithFormat:#"screen destination must not be empty"]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
else{
ListadoVideos *viewController = [self.storyboard instantiateViewControllerWithIdentifier:buttonSelected.viewControllerID];
viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
viewController.itemsCatalog = buttonSelected.viewControllerParameters;
[self.navigationController pushViewController:viewController animated:YES];
}
return;
}
just need to set all your buttonSelected.typeOfContent to "ViewController" and then replace your current code for this:
if ([buttonSelected.typeOfContent isEqualToString:#"ViewController"]) {
if ([[functions removeBlanksFromString:buttonSelected.viewControllerID] isEqualToString:#""]) {
[[[UIAlertView alloc] initWithTitle:#"Notification"
message:[NSString stringWithFormat:#"screen destination must not be empty"]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil] show];
}
else{
NSString *myUIViewControllerClassName = buttonSelected.viewControllerID;
Class myClass = NSClassFromString(myUIViewControllerClassName);
NSObject *myObject = [myClass new];
if( [myObject isKindOfClass:[UIViewController class]] ){
UIViewController *rightViewController = (UIViewController *) myObject;
rightViewController = [self.storyboard instantiateViewControllerWithIdentifier:myUIViewControllerClassName];
rightViewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:LOGO_COMPANY]];
[self.navigationController pushViewController:rightViewController animated:YES];
}
}
return;
}

iOS UIAlertView Other Button Action

I have this code currently for my UIAlertView:
if (counter > highscore) { //if highscore is achieved show an alert
highscore = counter;
NSString *nssHighscore = [NSString stringWithFormat:#"%i", highscore];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Congratulations, You Got A New High Score!!"
message:nssHighscore
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:#"Share"];
[alert show];
}
I want to run this code when the 'Share' button is clicked on the Alert
- (IBAction)PostToFacebook:(id)sender {
NSString *nssHighscore = [NSString stringWithFormat:#"%i", highscore];
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"My New Highscore is %d - Try to Beat it!, highscore]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
So far I've been following guides and questions on stack overflow but can't get it working.
Thanks
You'll want to use the alertView:clickedButtonAtIndex: delegate method, and check that the index matches [alertView firstOtherButtonIndex].
Like this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Congratulations, You Got A New High Score!!"
message:nssHighscore
delegate:self // <== changed from nil to self
cancelButtonTitle:#"Ok"
otherButtonTitles:#"Share"];
And in the same class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView firstOtherButtonIndex]) {
[self PostToFacebook:self];
}
}

Two UIAlertView consecutively in didFinishLaunchingWithOptions

I want two alert views to show up only when the user opens my application for the first time -- the second to appear after the first is dismissed. I have it set up to only show the UIAlertViews when it has not been shown before and I do not need help with this. I need help figuring out how to display two alert views in a row when this is the case.
-(void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex does not work for me.
Here is the code I have -- remember this is in didFinishLaunchingWithOptions:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL didFirstLaunch = [defaults boolForKey:#"DidFirstLaunch"];
if (!didFirstLaunch) {
[defaults setBool:YES forKey:#"DidFirstLaunch"];
UIAlertView *successAlert = //not important
[successAlert show];
[successAlert release];
//Somehow show second alert after the first is dismissed
}
I'm gonna post a very simple solution using GCD & blocks (GCD part is just in case the alert view is created on another thread then the main thread, callback should be safe to perform on the main thread). Remember, I just coded this in like 5 mins, so you definitely should work on improving the code. One thing that's a bit ugly is the delegate parameter that is overridden in my subclass. The interface of the subclass could be changed a bit to make it more obvious of what happens ...
Anyway, here goes ...
First create a subclass of UIAlertView, make it look somewhat like the following ...
#interface FSAlertView () <UIAlertViewDelegate>
#property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex);
#end
#implementation FSAlertView
#synthesize dismissHandler = _dismissHandler;
- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler
{
self.dismissHandler = dismissHandler;
self.delegate = self;
[self show];
}
// Alert view delegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
dispatch_async(dispatch_get_main_queue(), ^ {
if (_dismissHandler)
{
_dismissHandler(buttonIndex);
}
});
}
Now in the app we can create alert views like the following ...
FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:#"Alert 1"
message:#"Some message"
delegate:nil
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Show 2nd Alert", nil];
[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) {
NSLog(#"button pressed: %d", buttonIndex);
if (buttonIndex == 1)
{
UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:#"Alert 2"
message:#"Hi!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert2 show];
}
}];
If i understand your question correctly , then this may help:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[firstAlert show];
[self performSelector:#selector(test:) withObject:firstAlert afterDelay:2];
[firstAlert release];
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
[secondAlert show];
[self performSelector:#selector(test:) withObject:secondAlert afterDelay:2];
[secondAlert release];
-(void)test:(UIAlertView*)alert{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
This will show two alert views one after the other.
NOTE: I am not sure if you are dismissing the alerts with cancel button so i am dismissing them automatically after few seconds.
Try this:
UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[firstAlert setTag:444];
[firstAlert show];
firstAlert = nil;
AlertView Delegate Method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
switch (alertView.tag) {
case 444:
{
//Cancel ButtonIndex = 0
if (buttonIndex == 1) {
UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:#"Title 2" message:#"Message2" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Dismiss", nil];
[secondAlert setTag:555];
[secondAlert show];
secondAlert = nil;
}
}
break;
case 555:
{
if (buttonIndex == 1) {
NSLog(#"Code Here");
}
}
break;
}
}