Delegating UIAlertView to another class / file? Not working? - objective-c

so I'm new to iOS development and I'm trying to delegate the button click event to another class. Whenever I click a button on the alert, the app crashes and I get an error saying Thread_1 EXC_BAD_ACCESS.
This is my code.
// theDelegateTester.h
#import <UIKit/UIKit.h>
#interface theDelegateTester : UIResponder <UIAlertViewDelegate>
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
Implementation..
// theDelegateTester.m
#import "theDelegateTester.h"
#implementation theDelegateTester
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"Delegated");
}
#end
And here's the implementation for my view file..
#import "appleTutorialViewController.h"
#import "theDelegateTester.h"
#interface appleTutorialViewController ()
- (IBAction)tapReceived:(id)sender;
#end
#implementation appleTutorialViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)tapReceived:(id)sender {
theDelegateTester *newTester = [[theDelegateTester alloc] init];
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Alert!" message:#"This is a delegated alert" delegate:newTester cancelButtonTitle:#"Close" otherButtonTitles:#"Cool!", nil];
[myAlert show];
}
#end

First of all, you should always start your class names with a capital letter, so you can differentiate between classes and instances or methods easily.
And you probably leak the delegate class. You should declare a strong/retained property TheDelegateTester *myDelegate in your view controller. Then in tapReceived: something like this:
- (IBAction)tapReceived:(id)sender {
if (!self.myDelegate) {
TheDelegateTester *del = [[TheDelegateTester alloc] init];
self.myDelegate = del;
[del release];
}
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:#"Alert!" message:#"This is a delegated alert" delegate:newTester cancelButtonTitle:#"Close" otherButtonTitles:#"Cool!", nil];
[myAlert show];
[myAlert release];
}

Related

How to fix '[LoginVC viewControllers]: unrecognized selector sent to instance'

Im trying to use split view controllers for iphone but when i add the code for AppDelegate.m as below, i get the error. My app also utilises a login system using file LoginVC and that file was working fine before.
[Storyboard image here][1]: https://i.stack.imgur.com/GRosR.png
#import "AppDelegate.h"
#import "RightVC.h"
#import "LeftVC.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UISplitViewController *splitViewController = (UISplitViewController
*)self.window.rootViewController;
UINavigationController *leftNavController =
[splitViewController.viewControllers objectAtIndex:0];
LeftVC *leftViewController = (LeftVC *)[leftNavController
topViewController];
RightVC *rightViewController = [splitViewController.viewControllers
objectAtIndex:1];
Recipe *firstRecipe = [[leftViewController recipes] objectAtIndex:0];
[rightViewController setRecipe:firstRecipe];
return YES;
}
And my LoginVC.m
#import "LoginVC.h"
#interface LoginVC ()
#end
#implementation LoginVC
- (void)viewDidLoad {
[super viewDidLoad];
self.checkLogin = [[LoginService alloc]init];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) alertStatus:(NSString *)msg :(NSString *) title: (int) tag {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil,
nil];
alertView.tag = tag;
[alertView show];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little
preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)btnLogin:(id)sender {
if([self.txtUsername.text isEqualToString:#""] || [self.txtPassword.text
isEqualToString:#""]) {
[self alertStatus:#"Username and/or Password cannot be blank!"
:#"Sign in Failed" :0];
} else{
self.checkLogin.strUsername = self.txtUsername.text;
self.checkLogin.strPassword = self.txtPassword.text;
BOOL lgs = self.checkLogin.success;
if(lgs==1){
[self performSegueWithIdentifier:#"ShowMain" sender:self];
}else{
[self alertStatus:#"Wrong Username and/or Password!" :#"Sign in
Failed":0];
}
}
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.txtUsername resignFirstResponder];
[self.txtPassword resignFirstResponder];
[[self.view window] endEditing:YES];
}
#end
Been trying to fix but all i have are a few split view guides on the net

UIAlertViewDelegate in separate class crashes application

I'm having difficulties with UIAlertView delegation in class other than ViewController.
Everything is fine until user clicks the OK button - then app crashes with
Thread 1: EXC_BAD_ACCESS (code=2, address 0x8)
ViewController.h:
#import <UIKit/UIKit.h>
#import "DataModel.h"
#interface ViewController : UIViewController
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
DataModel *dataModel = [[DataModel alloc] init];
[dataModel ShowMeAlert];
[super viewDidLoad];
}
#end
DataModel.h
#import <Foundation/Foundation.h>
#interface DataModel : NSObject <UIAlertViewDelegate>
- (void)ShowMeAlert;
#end
DataModel.m
#import "DataModel.h"
#implementation DataModel
- (void)ShowMeAlert;
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Info" message:#"View did load!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
#pragma mark - UIAlertView protocol
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"Index: %d", buttonIndex);
}
#end
If code for showing alert and it's delegation methods is in ViewController - works perfectly.
When I remove UIAlertDelegation method ...didDismissWithButtonIndex... -
works without delegation.
When I set UIAlertView delegate to nil -
works without delegation.
Any clues what's wrong?
In this method:
- (void)viewDidLoad
{
DataModel *dataModel = [[DataModel alloc] init];
[dataModel ShowMeAlert];
[super viewDidLoad];
}
you are allocating a DataModel local variable which will be deallocated by ARC at the end of the scope. Hence, when dismiss is executed, your delegate is not there anymore. The fix for this is to store your DataModel in a strong property of your view controller. This way it will not be deallocated. The you would do:
- (void)viewDidLoad
{
self.dataModel = [[DataModel alloc] init];
[self.dataModel ShowMeAlert];
[super viewDidLoad];
}

UIAlertView with a user supplied context and [self autorelease]

I have looked over some ideas for how to supply a context to a UIAlertView. The common answers are save it in a dictionary or subclass UIAlertView. I don't like the idea of saving the context in a dictionary, it's the wrong place for the data. Subclassing UIAlertView is not supported by Apple, so by my standard, is not a good solution.
I came up with an idea, but I'm not sure what to make of it. Create an instance of a context object that is the delegate of UIAlertView. The alert view context, in turn, has it's own delegate which is the view controller.
The trouble is releasing memory. I set alertView.delegate to nil and call [self autorelease] to free the context object in -alertView:didDismissWithButtonIndex:.
THE QUESTION IS: What problems am I causing myself? I have a suspicion that I'm setting myself up for a subtle memory error.
Here is the simple version which only supports -alertView:clickedButtonAtIndex:
Use
- (void)askUserIfTheyWantToSeeRemoteNotification:(NSDictionary *)userInfo
{
[[[[UIAlertView alloc] initWithTitle:[userInfo valueForKey:#"action"]
message:[userInfo valueForKeyPath:#"aps.alert"]
delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]
cancelButtonTitle:#"Dismiss"
otherButtonTitles:#"View", nil] autorelease] show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context
{
if (buttonIndex != alertView.cancelButtonIndex)
[self presentViewForRemoteNotification:context];
}
Interface
#protocol WantAlertViewContextDelegate <NSObject>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context;
#end
#interface WantAlertViewContext : NSObject <UIAlertViewDelegate>
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context;
#property (assign, nonatomic) id<WantAlertViewContextDelegate> delegate;
#property (retain, nonatomic) id context;
#end
Implementation
#implementation WantAlertViewContext
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context
{
self = [super init];
if (self) {
_delegate = delegate;
_context = [context retain];
}
return self;
}
- (void)dealloc
{
[_context release];
[super dealloc];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.delegate alertView:alertView clickedButtonAtIndex:buttonIndex withContext:self.context];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
alertView.delegate = nil;
[self autorelease];
}
#synthesize delegate = _delegate;
#synthesize context = _context;
#end
You can use the concept of associated objects. Using the functions objc_setAssociatedObject() and objc_getAssociatedObject(). You can use these properties to essentially add a new property, in your case to hold an NSDictionary, to an object through a category.
Here is an example of a UIAlertView category. These files should be compiled without ARC, -fno-objc-arc flag set if the project is using ARC.
UIAlertView+WithContext.h:
#import <UIKit/UIKit.h>
#interface UIAlertView (Context)
#property (nonatomic, copy) NSDictionary *userInfo;
#end
UIAlertView+WithContext.m:
#import "UIAlertView+WithContext.h"
// This enum is actually declared elseware
enum {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
};
#implementation UIAlertView (Context)
static char ContextPrivateKey;
-(void)setUserInfo:(NSDictionary *)userInfo{
objc_setAssociatedObject(self, &ContextPrivateKey, userInfo, 3);
}
-(NSDictionary *)userInfo{
return objc_getAssociatedObject(self, &ContextPrivateKey);
}
#end
This category is easily used.
SomeViewController.m: a UIAlertViewDelegate using ARC or not.
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alert.userInfo = [NSDictionary dictionaryWithObject:#"Hello" forKey:#"Greeting"];// autorelease if MRC
[alert show]; // release if MRC
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(#"userInfo:%#",alertView.userInfo);
}
When you press the alertview's OK button you will see:
userInfo:{
Greeting = Hello;
}
A couple of notes:
1) Make sure the association type matches the property declaration so things behave as expected.
2) You probably shouldn't use userInfo for the property/association since Apple may well decide to add a userInfo property to UIAlertView in the future.
Edit To address your concerns about your [self autorelease];
It is imperative that you balance your implicit alloc retain from this line: delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]. You achieve this balance by calling [self autorelease]; in the final UIAlertView delegate method.
Granted, this does feel wrong. Mostly because there is no way when looking at this that it doesn't at first blush look like memory mis-management. But there is one simple way to avoid this "controlled leak" API you are creating; Have the instance of WantAlertViewContext explicitly retain itself. For example:
-(id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context{
self = [super init];
if (self) {
_delegate = delegate;
_context = [context retain];
}
return [self retain]; // Explicitly retain self
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
alertView.delegate = nil;
[self autorelease]; // Or just [self release]; doesn't make much difference at this point
}
Now your class has some internal harmony. I say some because this is still not perfect. For example, if an instance is never an alert-view delegate it will never be released. It is still just a "semi-controlled" memory leak.
Anyway, now your instantiation call can look more logical:
delegate:[[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo] autorelease];
I think that this particular design pattern is fraught with danger. If you do end up using it keep a close eye on it.
I've come up with a simpler solution that may fit in some circumstances. Because you get the NSAlertView context when the delegate gets called, I use the actual address of the object to make a tag (NSString*) which I then use to store custom values in a global or object specific NSDictionary. Here is an example:
+(NSString*)GetTag:(id)ObjectIn
{
return [NSString stringWithFormat:#"Tag-%i",(int)ObjectIn];
}
In the Delegate:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString* MyID = [CommandManager GetTag:alertView];
[CurrentActiveAlerts removeObjectForKey:MyID];
}
Calling:
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:title_text
message:#""
delegate:self
cancelButtonTitle:nil
otherButtonTitles:button_text ,nil];
CurrentActiveAlerts[[CommandManager GetTag:myAlert]] = CommandToRun; // Querky way to link NSDict to UIAlert, but the best I could think of
[myAlert show];
[myAlert release];
The keys will end up looking like "Tag-226811776". Hope this helps.

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

Best Technique for Replacing Delegate Methods with Blocks

I'm looking to create a category to replace delegate methods with callbacks blocks for a lot of the simple iOS APIs. Similar to the sendAsyc block on NSURLConnection. There are 2 techniques that are leak free and seem to work fine. What are the pros/cons about each? Is there a better way?
Option 1. Use a category to implement the delegate's callback method on NSObject with the external callback block scoped.
// Add category on NSObject to respond to the delegate
#interface NSObject(BlocksDelegate)
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
#end
#implementation NSObject(BlocksDelegate)
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Self is scoped to the block that was copied
void(^callback)(NSInteger) = (id)self;
// Call the callback passed if
callback(buttonIndex);
[self release];
}
#end
// Alert View Category
#implementation UIAlertView (BlocksDelegate)
+ (id) alertWithTitle:(NSString*)title
message:(NSString*)message
clickedBlock:(void(^)(NSInteger))buttonIndexClickedBlock
cancelButtonTitle:(NSString*)cancelButtonTitle
otherButtonTitles:(NSString*)otherButtonTitles
{
// Copy block passed in to the Heap and will stay alive with the UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:[buttonIndexClickedBlock copy]
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles, nil];
// Display the alert
[alert show];
// Autorelease the alert
return [alert autorelease];
}
#end
This adds a lot of methods on the NSObject and seems like it could cause issues with any other class trying to use the standard delegate method. But it keeps the block alive with the object and returns the callback without any leaks that I've found.
Option 2. Create an light-weight class to contain the block, dynamicly associate it with the class so it will stay in the heap and remove it when the callback is complete.
// Generic Block Delegate
#interface __DelegateBlock:NSObject
typedef void (^HeapBlock)(NSInteger);
#property (nonatomic, copy) HeapBlock callbackBlock;
#end
#implementation __DelegateBlock
#synthesize callbackBlock;
- (id) initWithBlock:(void(^)(NSInteger))callback
{
// Init and copy Callback Block to the heap (#see accessor)
if (self = [super init])
[self setCallbackBlock:callback];
return [self autorelease];
}
- (void) dealloc
{
// Release the block
[callbackBlock release], callbackBlock = nil;
[super dealloc];
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Return the result to the callback
callbackBlock(buttonIndex);
// Detach the block delegate, will decrement retain count
SEL key = #selector(alertWithTitle:message:clickedBlock:cancelButtonTitle:otherButtonTitles:);
objc_setAssociatedObject(alertView, key, nil, OBJC_ASSOCIATION_RETAIN);
key = nil;
// Release the Alert
[alertView release];
}
#end
#implementation UIAlertView (BlocksDelegate)
+ (id) alertWithTitle:(NSString*)title
message:(NSString*)message
clickedBlock:(void(^)(NSInteger))buttonIndexClickedBlock
cancelButtonTitle:(NSString*)cancelButtonTitle
otherButtonTitles:(NSString*)otherButtonTitles
{
// Create class to hold delegatee and copy block to heap
DelegateBlock *delegatee = [[__DelegateBlock alloc] initWithBlock:buttonIndexClickedBlock];
[[delegatee retain] autorelease];
// Create delegater
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:delegatee
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:otherButtonTitles, nil];
// Attach the Delegate Block class to the Alert View, increase the retain count
objc_setAssociatedObject(alert, _cmd, delegatee, OBJC_ASSOCIATION_RETAIN);
// Display the alert
[alert show];
return alert;
}
#end
I like that this doesn't add anything on top of NSObject and things are a little more separated. It's attaching to the instance via the address of the function.
I had a similar problem and chose your option 2, but with the 2 small additions:
Explicitly marking the delegate it implements like this:
#interface __DelegateBlock:NSObject <BlocksDelegate>
Check to ensure the callback is not nil before calling:
if (callbackBlock != nil) {
callbackBlock(buttonIndex);
}
Here's what I did:
typedef void(^EmptyBlockType)();
#interface YUYesNoListener : NSObject <UIAlertViewDelegate>
#property (nonatomic, retain) EmptyBlockType yesBlock;
#property (nonatomic, retain) EmptyBlockType noBlock;
+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock;
#end
#implementation YUYesNoListener
#synthesize yesBlock = _yesBlock;
#synthesize noBlock = _noBlock;
- (id) initWithYesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
self = [super init];
if (self)
{
self.yesBlock = [[yesBlock copy] autorelease];
self.noBlock = [[noBlock copy] autorelease];
}
return self;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0 && self.noBlock)
self.noBlock();
else if (buttonIndex == 1 && self.yesBlock)
self.yesBlock();
[_yesBlock release];
[_noBlock release];
[alertView release];
[self release];
}
- (void) alertViewCancel:(UIAlertView *)alertView
{
if (self.noBlock)
self.noBlock();
[_yesBlock release];
[_noBlock release];
[alertView release];
[self release];
}
+ (void) yesNoWithTitle:(NSString*)title message:(NSString*)message yesBlock:(EmptyBlockType)yesBlock noBlock:(EmptyBlockType)noBlock
{
YUYesNoListener* yesNoListener = [[YUYesNoListener alloc] initWithYesBlock:yesBlock noBlock:noBlock];
[[[UIAlertView alloc] initWithTitle:title message:message delegate:yesNoListener cancelButtonTitle:#"No" otherButtonTitles:#"Yes", nil] show];
}
#end