What's a simple way to get a text input popup dialog box with Actionsheet picker on an iPhone - objective-c

want to get input from pickerView in textField when alert show Objective C

NOTE: Opening actionsheet from alertview first dismiss your alertview then open actionsheet.My code may help you regarding opening actionsheet from alertview
#import "ViewController.h"
#interface ViewController ()<UITextFieldDelegate,UIActionSheetDelegate>
{
NSString *alertSelectValue;
UITextField * alertTextField;
}
#end
#implementation ViewController
- (void)viewDidLoad {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Hello!" message:#"Please enter your name:" delegate:self cancelButtonTitle:#"Continue" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.tag=101;
alertTextField.delegate=self;
alertTextField.keyboardType = UIKeyboardTypeNumberPad; alertTextField.placeholder = #"Enter your name";
[alert show];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField.tag==101)
{
[textField setUserInteractionEnabled:YES];
[textField resignFirstResponder];
NSString*String = [NSString stringWithFormat:#"%#",textField.text];
[self openSheet:String];
}
}
-(void)openSheet:(NSString*)text
{
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:text,#"Test" ,nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
UIViewController *topvc=[self topMostController];
[actionSheet showInView:topvc.view];
}
//-------- Get top Most view to -----
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(#"Index = %d - Title = %#", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
if(buttonIndex == 0)
{
alertTextField.text= [actionSheet buttonTitleAtIndex:buttonIndex];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Related

after calling an func for init the UIAlertController remains nil

I have 2 alerts I want to present in different cases, I wrote a general function to init the alerts in the beginning and change the messages later, but when I am trying to present the alert I get a crash. When I inspect the notesAlert in runtime it is still nil.
Can someone explain what I did wrong?
#interface viewController (){
UIAlertController *tableAlert;
UIAlertController *notesAlert;
}
#end
#implementation viewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initAlert:tableAlert];
[self initAlert:notesAlert];
}
// func to init the alerts
-(void)initAlert:(UIAlertController*)alert{
alert = [UIAlertController alertControllerWithTitle: #"" message: #"" preferredStyle:UIAlertControllerStyleActionSheet];
[alert setModalPresentationStyle:UIModalPresentationPopover];
[alert.popoverPresentationController setSourceView:self.view];
UIPopoverPresentationController *popover = [alert popoverPresentationController];
CGRect popoverFrame = CGRectMake(0,0, self.view.frame.size.width/2, self.view.frame.size.width/2);
popover.sourceRect = popoverFrame;
UIAlertAction *dismiss = [UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:dismiss];
}
- (IBAction)showNotes:(id)sender {
// here the notesAlert is still nil
[notesAlert setTitle:#"oops"];
[notesAlert setMessage:#"you pressed the wrong one"];
[self presentViewController:notesAlert animated:YES completion:nil];
}
#end
[self initAlert: notesAlert]; doesn't create notesAlert. Instead, you could use notesAlert = [self initAlert];
Maybe something like this:
#interface ViewController () {
UIAlertController *tableAlert;
UIAlertController *notesAlert;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableAlert = [self initAlert];
self.notesAlert = [self initAlert];
}
// func to init the alerts
- (UIAlertController *) initAlert {
UIAlertController *alert = [UIAlertController alertControllerWithTitle: #"" message: #"" preferredStyle: UIAlertControllerStyleActionSheet];
[alert setModalPresentationStyle:UIModalPresentationPopover];
[alert.popoverPresentationController setSourceView: self.view];
UIPopoverPresentationController *popover = [alert popoverPresentationController];
CGRect popoverFrame = CGRectMake(0,0, self.view.frame.size.width/2, self.view.frame.size.width/2);
popover.sourceRect = popoverFrame;
UIAlertAction *dismiss = [UIAlertAction actionWithTitle: #"Ok" style: UIAlertActionStyleDefault handler:nil];
[alert addAction: dismiss];
return alert;
}

UIModalViewController Hidden behind a UIViewController

I am working with a sliding menu, like you would see in the Facebook or Path app.
In the rear menu I am wanting to allow the user to add an image by using the camera or photo library.
The issue I am having is the main view controller, the one that I slide, is always on top.
The Action Sheet presents just fine, but the modal view controller is hidden partially by the main view controller.
Here's my picker code:
- (void)viewDidLoad
{
[super viewDidLoad];
_UIPicker = [[UIImagePickerController alloc] init];
_UIPicker.delegate = self;
}
- (IBAction)profilePicButtonTapped
{
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:#"Set Up Your Profile Picture" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Camera", #"Photo Library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[actionSheet showInView:_rearTableView];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
_UIPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_UIPicker animated:YES completion:nil];
}
else if (buttonIndex == 1)
{
_UIPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_UIPicker animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Error" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage * selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
_profileImageView.image = selectedImage;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
A quick way to solve it would be to hide the main ViewController when you present the picker and show it again once it's dismissed. Assuming you have a reference to the main ViewController, just set it's view.hidden property to YES.
Another way to handle it would be to present it from the main ViewController instead of the rear menu.

Alert View button actions not working

I am using an Alert view to bring up a popup where the user will choose to either select a photo from the library or take a photo to use. The alert view comes up fine, but when I select a button the code I have implemented is not run?!?
for some reason the - (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex does not seem to even get run!? I'm lost run over lots of tutorials and websites but can't see why?! please help!
code:
.m
#import "LoadViewController.h"
#implementation LoadViewController
int imageCase;
- (IBAction)pick:(id) sender {
imageCase = [sender tag];
UIAlertView *picturePopup = [[UIAlertView alloc]
initWithTitle:#"Select Photo" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Choose From Library", #"Take Photo", nil];
[picturePopup show];
}
- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"***************getting here****************");
if (buttonIndex == 1) {
NSLog(#"***************library****************");
//Library Picker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
if (buttonIndex == 2) {
NSLog(#"***************camera****************");
//Camera
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
switch (imageCase) {
case 1:
imageView1.image = image;
break;
case 2:
imageView2.image = image;
break;
}
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker.parentViewController dismissModalViewControllerAnimated:YES];
}
#end
.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface LoadViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIAlertViewDelegate> {
IBOutlet UIImageView *imageView1;
IBOutlet UIImageView *imageView2;
}
- (IBAction)pick:(id) sender;
#end
You need to use the actual delegate method:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
This is not the same as:
- (void)picturePopup:(UIAlertView *)picturePopup clickedButtonAtIndex:(NSInteger)buttonIndex
The method signatures are different. alertView:clickedButtonAtIndex: vs. picturePopup:clickedButtonAtIndex:
You can rename the variable you just can not change the method signature.
This is sort of off topic, but I notice you didn't synthesize *imageView1 and *imageView2. lol

Open an UIAlertView again from its own delegate fails in iOS

I have an iPhone App that opens an UIAlertView with an UITextInput field and a button. When the button gets pressed the delegate method should do a validation of the input and on success proceed app execution or open the same UIAlertView again on failure.
Now I reduced it to this litte test class with two UIAlertViews. It is always possible to open the other view but when I hit the button it asks for the screen remains empty.
#import "Yesorno.h"
#implementation Yesorno
#synthesize promptYES, promptNO;
- (id)init {
self = [super init];
if (self) {
promptYES = [[UIAlertView alloc] init];
[promptYES setDelegate:self];
[promptYES setTitle:#"Press YES"];
[promptYES addButtonWithTitle:#"YES"];
[promptYES addButtonWithTitle:#"NO"];
promptNO = [[UIAlertView alloc] init];
[promptNO setDelegate:self];
[promptNO setTitle:#"Press NO!"];
[promptNO addButtonWithTitle:#"YES"];
[promptNO addButtonWithTitle:#"NO"];
}
return self;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
[promptYES show];
else
[promptNO show];
}
#end
Edit: here is the AppDelegate. It is now really a very basic application without any view controller
#import "AppDelegate.h"
#import "Yesorno.h"
#implementation AppDelegate
#synthesize window, yesorno;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
yesorno = [[Yesorno alloc] init];
[yesorno.promptYES show];
return YES;
}
#end
Any ideas how I could show the same dialog again? Thanks.
You should implement didDismissWithButtonIndex delegate method:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
[promptYES show];
else
[promptNO show];
}
You need to reimplement your - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex: delegate method.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView == promptYES)
{
if (buttonIndex == 0)
[promptYES show];
}
else if(alertView == promptNO)
{
if (buttonIndex == 0)
[promptNO show];
}
}

How to use a UIAlert to open a NIB or UIView

Here is my code so far I can get a URL to work but I want to load a nib from this Alert tab
- (IBAction)aboutAction { // The action called when the about button is clicked.
UIAlertView * aboutView = [[UIAlertView alloc] initWithTitle:#"Alert:" // Create a new UIAlertView named aboutScreen, and allocate it. Set the title to "About"
message:#"MESSAGE GOES HERE"
delegate:self
cancelButtonTitle:#"Got It Thanks!"
otherButtonTitles:#"Donate Now", nil];
[aboutView show]; // Show the UIAlertView on the screen.
[aboutView release]; // Release the UIAlertView from the memory.
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex == 1) { //Not sure what to do here.
}
}
-(void) alertView:(UIAlertView *) alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 1: {
Accidenthelpercall *help = [[Accidenthelpercall alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:help animated:NO];
}
case 2: {
Accidentdamagecar *damagecar = [[Accidentdamagecar alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:damagecar animated:NO];
}
}
-(void) alertView:(UIAlertView *) alertview clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 1: {
Accidenthelpercall *help = [[Accidenthelpercall alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:help animated:NO];
[help release];
}
case 2: {
Accidentdamagecar *damagecar = [[Accidentdamagecar alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:damagecar animated:NO];
[damagecar release];
}
}