Reputation: 341
I am new to Objective C and having an issue that I know must be a very simple one - I think perhaps I am approaching it the wrong way.
I have created an UIAlertViewDelegate class that I want to be the delegate for some UIAlertViews that I have in my View Controller class.
When the user presses a button and enters some text, I would like a label in my ViewController class to be updated with that text. However, I obviously cannot reference the label "outputLabel" from my UIAlertViewDelegate class unless I pass it in some way.
The error I get is "Use of undeclared identifier 'outputLabel'. "
ViewController.h
@interface ViewController : UIViewController <UIAlertViewDelegate>;
...
@property (strong) UIAlertViewDelegate *AVDelegate;
ViewController.m
- (void)viewDidLoad
{
UIAlertViewDelegate *AVDelegate;
AVDelegate = [[UIAlertViewDelegate alloc] init];
[super viewDidLoad];
}
- (IBAction)doAlertInput:(id)sender {
UIAlertViewDelegate *AVDelegate;
AVDelegate = [[UIAlertViewDelegate alloc] init];
UIAlertView *alertDialogue;
alertDialogue = [[UIAlertView alloc]
initWithTitle:@"Email Address"
message:@"Please Enter Your Email Address:"
delegate:self.AVDelegate
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
alertDialogue.alertViewStyle=UIAlertViewStylePlainTextInput;
[alertDialogue show];
}
UIAlertViewDelegate.m
#import "UIAlertViewDelegate.h"
@implementation UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
NSString *alertTitle = [alertView title];
if ([alertTitle isEqualToString:@"Alert Button Selected"] || [alertTitle isEqualToString:@"Alert Button Selected"]) {
if ([buttonTitle isEqualToString:@"Maybe Later"])
{
outputLabel.text=@"User Clicked: 'Maybe Later'";
UIAlertViewDelegate.h
@interface UIAlertViewDelegate : UIViewController <UIAlertViewDelegate,UIActionSheetDelegate>;
I've done some work many years ago with Java. I imagine I have to pass my UIAlertViewDelegate class the view somehow but Im not sure on the syntax, would greatly appreciate a pointer in the right direction...
James
Upvotes: 2
Views: 229
Reputation: 886
Your view controller is the delegate for the alert view, therefore, you don’t need to crate the delegate object.
init the alert view using self (the view controller) as the delegate argument.
You then implement the delegate methods (for the alert view) within the view controller.
The point of delegation is to make it possible for an object to have callbacks on any class.
I would suggest reading over protocols and delegation.
I'm guessing you are trying to use the alert view to input text, so you would set the alert view style to UIAlertViewPlainTextStyle, and setting the label in one of the callbacks by accessing the text field from the alertView object.
Upvotes: 0
Reputation: 31026
There are a variety of things here that you shouldn't be doing but, starting with the main ones affecting your question....
You shouldn't be inventing your own UIAlertViewDelegate
class, which means don't do this...
@interface UIAlertViewDelegate : UIViewController <UIAlertViewDelegate,UIActionSheetDelegate>;
Because UIAlertViewDelegate
is a protocol, you also can't use alloc
with it.
The main thing that's correct is...
@interface ViewController : UIViewController <UIAlertViewDelegate>
That says that your ViewController
is able to act as a UIAlertViewDelegate
and therefore your code for creating the alert can use delegate:self
instead of trying to create a separate delegate property or object.
You are likely to still have questions but those changes should clean it up a bit.
Upvotes: 1