UItextField - iOS multiple line textfield on AlertView - objective-c

Here is my code
I am adding uitextfield to alertview , what I need is to get the data from the array to uitextfield with multiple lines and should be editable.
NSString *selected = [lineItemsArray objectAtIndex:indexPath.row];
UIAlertController *controller = [UIAlertController alertControllerWithTitle:#"Notice" message:#"Selected" preferredStyle:UIAlertControllerStyleAlert];
[controller addTextFieldWithConfigurationHandler:^(UITextField *linetextField)
{
linetextField.text=selected;
}];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:#"Okay"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES];
NSLog(#"Dismiss button tapped!");
}];
[controller addAction:alertAction];
[self presentViewController:controller animated:YES completion:nil];
PackageLineItemViewController *pLineVC = [[PackageLineItemViewController alloc]init];
pLineVC.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:pLineVC animated:YES];
[self.view endEditing:YES];

I tried the solution for your question.If you use alertViewController with textField you can't set multiple line.For label you can set multiple line but for textfield you can't set.
#import "ViewController.h"
#interface ViewController ()<UITextFieldDelegate>
{
NSArray *arr;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arr = [NSArray arrayWithObjects:#"iOS",#"Android",#"Windows",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionShowClickTextField:(id)sender
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:#"Enter your text"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.delegate = self;
textField.text = #"";
for(int i=0;i<[arr count];i++)
{
textField.text = [textField.text stringByAppendingString:[NSString stringWithFormat:#"%#,\n",[arr objectAtIndex:i]]];
}
NSLog(#"The textField text is - %#",textField.text);
}];
[self presentViewController:alertController animated:YES completion:nil];
UIAlertAction *actionAlert = [UIAlertAction actionWithTitle:#"Save"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// access text from text field
NSString *text = ((UITextField *)[alertController.textFields objectAtIndex:0]).text;
NSLog(#"The text is- %#",text);
}];
actionAlert.enabled = YES;
[alertController addAction:actionAlert];
}
#end
The printed output results are
The textField text is - iOS, Android, Windows

UITextFIeld is specific to one line. you cannot have multi-line textfeild. IF you need multi line , you should go for UITextView. Again you cannot add UITextView to the alert controller. You need to build a custom alert view.

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;
}

In objective-c, by using self in a view controller, does this create a strong reference cycle?

I've noticed while working with Objective-C, the compiler throws error symbols enforcing the use of either self or an underscore when using a property which I think doesn't happen as harshly when using Swift. I'm now at the crossroads where I believe my view controller isn't being deallocated from the navigation stack. I've used self pretty heavily in order to silence the error symbols but I don't know if this has created retain cycles or not?
#import "MenuController.h"
#import "AppDelegate.h"
#import "CMUser.h"
#import "NotificationsSettingsController.h"
#import "PersonalInfoController.h"
#import "BeLive-Swift.h"
#import "LoginAndSecurityController.h"
#import "SplashController.h"
#define kCellSeparatorTag 100
#implementation SideMenuCell
- (void)layoutSubviews{
[super layoutSubviews];
}
#end
#interface MenuController ()<UITableViewDataSource ,UITableViewDelegate>
#property (nonatomic, strong) NSArray *menuTitles;
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UILabel *usersNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *usersEmailLabel;
#property (weak, nonatomic) IBOutlet UILabel *addPhotoLabel;
#property (weak, nonatomic) IBOutlet SettingsHeaderViewWithPhoto *settingsHeaderViewWithPhoto;
#end
#implementation MenuController
NSString *userFirstNameString;
NSString *userLastNameString;
NSString *userEmailString;
NSMutableString * usersFullNameString;
+ (instancetype)controller{
static MenuController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = ControllerFromMainStoryBoard([self description]);
});
return sharedInstance;
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.settingsHeaderViewWithPhoto.backButton addTarget:self action:#selector(popViewCon:) forControlEvents:UIControlEventTouchUpInside];
[self.settingsHeaderViewWithPhoto.rightButton addTarget:self action:#selector(logoutTapped:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)showUserInfoInHeaderView{
if (userEmailString.length != 0) {
self.settingsHeaderViewWithPhoto.emailSubLabel.text = userEmailString;
} else {
self.settingsHeaderViewWithPhoto.emailSubLabel.text = #"Email not available";
}
if (usersFullNameString.length != 0) {
self.settingsHeaderViewWithPhoto.nameLabel.text = usersFullNameString;
} else {
self.settingsHeaderViewWithPhoto.nameLabel.text = #"Full name not available";
}
[self setMenuArrayBasedOnUserType];
[self.tableView reloadData];
}
-(void)setMenuArrayBasedOnUserType{
if (CMUser.currentUser.type == UserTypeArtist) {
self.menuTitles = #[#[#"Notifications", #"Stripe Info", #"Personal Info", #"Login and Security", #"Invite a Friend", #"Help", #"Legal Agreement"]];
[self.tableView reloadData];
} else if (CMUser.currentUser.type == UserTypeViewer){
self.menuTitles = #[#[#"Notifications", #"Personal Info", #"Login and Security", #"Invite a Friend", #"Help", #"Legal Agreement"]];
[self.tableView reloadData];
} else {
[self showLoggedInAlert];
};
}
-(void)showLoggedInAlert{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Session Time Out"
message:#"You must be logged in first."
preferredStyle:UIAlertControllerStyleAlert]; // 1
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(#"You pressed ok");
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SplashController *vc = [sb instantiateViewControllerWithIdentifier:#"SplashController"];
[self presentViewController:vc animated:YES completion:nil];
}];
[alert addAction:firstAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)getLoggedInUserInfo{
[NetworkManager callEndPoint:USER_DETAILS withDict:nil method:#"GET" JSON:YES success:^(id responseObject) {
userFirstNameString = responseObject[#"data"][0][#"firstname"];
userLastNameString = responseObject[#"data"][0][#"lastname"];
userEmailString = responseObject[#"data"][0][#"emailAddress"];
usersFullNameString = [NSMutableString stringWithString:userFirstNameString];
[usersFullNameString appendString:#" "];
[usersFullNameString appendString: userLastNameString];
[self showUserInfoInHeaderView];
} failure:^(id responseObject, NSError *error) {
NSLog(#"callEndPoint error is: %#", error);
}];
}
-(void)showProfilPhoto{
if ([CMUser currentUser][#"imageUrl"] != NULL) {
self.settingsHeaderViewWithPhoto.addPhotoLabel.text = #"";
[self.settingsHeaderViewWithPhoto.userPhotoButton sd_setImageWithURL:[NSURL URLWithString:[CMUser currentUser][#"imageUrl"]]forState:UIControlStateNormal];
} else {
self.settingsHeaderViewWithPhoto.addPhotoLabel.text = #"Add Photo";
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBarHidden = YES;
[self.tableView registerClass:[SettingsTableViewCell class] forCellReuseIdentifier:#"MenuCell"];
self.tableView.backgroundColor = [UIColor colorWithHex:0x222222];
[self getLoggedInUserInfo];
[self showProfilPhoto];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
CGRect frame = self.view.frame;
frame.size.width = revealWidth;
self.view.frame = frame;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.menuTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray *menuItems = self.menuTitles[section];
return menuItems.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *menuItems = self.menuTitles[indexPath.section];
SettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MenuCell"
forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = menuItems[indexPath.row];
UIView *separatorView = [cell.contentView viewWithTag:kCellSeparatorTag];
if (!separatorView) {
CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
separatorView = [[UIView alloc] initWithFrame:CGRectMake(10, 43, revealWidth - 20, 1)];
separatorView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:separatorView];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath
animated:YES];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
[self notificationsTapped];
default:
break;
}
switch (indexPath.row) {
case 1:
//TODO: IF userrole == artist then show stripe info else if userrole == , show person information
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show strip credit info");
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show personal info");
[self personalInfoTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 2:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show strip personal info");
[self personalInfoTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show login and security");
[self loginSecurityTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 3:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show login and security");
[self loginSecurityTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show Invite a friend");
[self inviteFriendTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 4:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show Invite a friend");
[self inviteFriendTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show Help");
//goto: webURL www.belive.com/help
[self helpTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 5:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show help");
//goto: webURL www.belive.com/help
[self helpTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show legal agreement");
//goto: webURL www.belive.com/legal
[self legalAgreementTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 6:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show legal agreement");
//goto: webURL www.belive.com/legal
[self legalAgreementTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"do nothing because there is not a value for this case.");
}
break;
default:
break;
}
}
}
- (void)notificationsTapped{
NotificationsSettingsController *vc = [NotificationsSettingsController controller];
[self.navigationController pushViewController:vc
animated:YES];
}
- (void)popViewCon: (UIButton*)sender{
[ApplicationDelegate toggleMenu];
}
- (void)logoutTapped: (UIButton*)sender{
[ApplicationDelegate toggleMenu];
[CMUser logOut];
[ApplicationDelegate setController];
}
-(void)personalInfoTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
PersonalInfoController *vc = [sb instantiateViewControllerWithIdentifier:#"PersonalInfoController"];
vc.usersProfilePhoto = self.settingsHeaderViewWithPhoto.userPhotoButton.imageView.image;
[self.navigationController pushViewController:vc animated:YES];
}
-(void)loginSecurityTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
LoginAndSecurityController *vc = [sb instantiateViewControllerWithIdentifier:#"LoginAndSecurityController"];
//LoginAndSecurityController *vc = [LoginAndSecurityController controller];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)inviteFriendTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
InviteFriendController
*vc = [sb instantiateViewControllerWithIdentifier:#"InviteFriendController"];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)helpTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
HelpController *vc = [sb instantiateViewControllerWithIdentifier:#"helpController"];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)legalAgreementTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
LegalAgreementController *vc = [sb instantiateViewControllerWithIdentifier:#"legalController"];
[self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)addPhotoButtonTapped:(UIButton *)sender {
[self configureAddPhotoActionSheet];
}
-(void)configureAddPhotoActionSheet{
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped do nothing.
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Take a Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// take photo button tapped.
[self takePhoto];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Choose Photo from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// choose photo button tapped.
[self selectPhoto];
}]];
[self presentViewController:actionSheet animated:YES completion:nil];
}
- (void)takePhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)selectPhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
//UIImagePickerDelegate Methods
/*
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.profilePictureButton setImage:chosenImage forState:UIControlStateNormal];
self.addPhotoLabel.text = #"";
[self updateProfilePicture:chosenImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
} */
- (void)updateProfilePicture:(UIImage*)image{
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSString *imageString = [NSString stringWithFormat:#"data:image/png;base64,%#", [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
NSDictionary *params = #{#"ImageFileContent" : imageString,
#"ImageName" : #"image.jpg"
};
NSString *uploadEndPoint = #"artist/add-artist-photo";
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.settingsHeaderViewWithPhoto.userPhotoButton animated:YES];
[NetworkManager callEndPoint:uploadEndPoint
withDict:params
method:#"POST"
JSON:YES
success:^(id responseObject) {
NSLog(#"The response object has: %#", responseObject);
[CMUser currentUser][#"imageUrl"] = responseObject[#"data"][0][#"imageURL"];
[[CMUser currentUser] setCurrent];
[HUD hideAnimated:YES];
} failure:^(id responseObject, NSError *error) {
[HUD hideAnimated:YES];
}];
}
#end
This is used to synthesise the variable. It is necessary in objective c and swift works differently then swift. And it doesn't creat any retail cycle.
After doing some reading, I've come to the realization that retain cycles can be created by delegates, using self within a closure, and if other objects are referencing your view controller in question. The problem I had was that this particular view controller is actually the root view controller of a navigation stack in which other view controllers were referencing it as well. Also I made a call to self within two block statements in the code above which also has the potential for creating a strong reference to itself.

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

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

No known class method for selector presentViewController in UIAlertController

I know it is a very simple question and i am working on it since 3 hour but didn't able to make it working. I am trying to implement UIAlertController to show error message using Apple documentation, but i am getting error on this line that no known class method for selector presentViewController [self presentViewController:alert animated:YES completion:nil]; I searched and got many solutions but none is working here. AlertMessageViewController is my custom class, which is inherited from UIViewController.
AlertMessageViewController.h
#import <UIKit/UIKit.h>
#interface AlertMessageViewController : UIViewController
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle;
#end
AlertMessageViewController.m
#import "AlertMessageViewController.h"
#import <UIKit/UIKit.h>
#interface AlertMessageViewController ()
#end
#implementation AlertMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"title" message:#"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(#"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
#end
Why is your return type for showAlert: method instancetype? And you didn't return anything, it should be void.
EDIT: Also, your method shouldn't be Class method
this should work:
-(void)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"title" message:#"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(#"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
UPDATE:
Okay try this:
+(UIAlertController*)alertWithTitle: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle: preferredStyle];
UIAlertAction *ok =[UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(#"ok action");}];
[alert addAction:ok];
return alert;
}
}
Then when you want to show it, call it like this:
[self presentViewController:[AlertMessageViewController alertWithTitle:#"Title" withMessage:#"Message" preferredStyle:UIAlertControllerStyleAlert] animated:YES completion:NULL];

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];
}
}