UIAlertViewDelegate extended protocol - iphone

I'm trying to extend the UIAlertViewDelegate protocol with one optional delegate method.
interface
#protocol HPAlertViewDelegate;
#interface HPAlertView : UIAlertView<UIWebViewDelegate>{
id <HPAlertViewDelegate> delegate;
UIWebView *webView;
}
#property (nonatomic, assign) id <HPAlertViewDelegate> delegate;
#property (nonatomic, retain) UIWebView *webView;
- (id)initWithWebURL:(NSString *)url title:(NSString *)aTitle;
#end
#protocol HPAlertViewDelegate <UIAlertViewDelegate>
#optional
- (void)HPAlertViewWebViewDidLoad:(HPAlertView *)alertView;
#end
implementation
#dynamic delegate
when I use it in myViewController like:
HPAlertView *alertView = [[HPAlertView alloc] initWithWebURL:myURL tile:myTitle];
[alertView setDelegate:self];
I have 2 issues :
if I set delegate to #dynamic, my delegate in HPAlertView is always null even with setDelegate:self
if I set delegate to #synthesize, my delegate respond only to the new #optional delegate methods and not to UIAlertView delegate methods.

Try #synthesize and then implement the setter as this:
- (void)setDelegate:(id)aDelegate {
super.delegate = aDelegate;
delegate = aDelegate;
}
I guess #synthesize in your class will generate a new instance variables named delegate that is different from the super class delegate instance variable. You can read more about this here: http://cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html

Related

Passing data between view controllers using protocol and delegate

I am trying to push a string text to viewcontroller 1 from my viewcontroller 2 using protocols and delegate. I am new to this method of passing data so forgive me if I seem ignorant in any way. The string color always return null. I will post the code I have so far and if it helps, im using navigation controller and using the navigation back button to go from ViewController 2 to ViewController 1.
Viewcontroller 2
.h
#protocol PassString <NSObject>
#required
- (void) setSecondFavoriteColor:(NSString *)string;
#end
#interface ViewController2 : UIViewController{
UIButton *button;
NSString *ee
id <PassString> delegate;
}
#property (retain) id delegate;
ViewController 2
.m
#synthesize delegate;
-(void)button{
ee = #"Blue Color";
[[self delegate] setSecondFavoriteColor:ee];
ViewController 1.h
#interface ViewController1 : UIViewController <PassString>{
NSString*color;
}
#property (strong,nonatomic) NSString *color
ViewController 1.m
- (void)setSecondFavoriteColor:(NSString *)string
{
color = string;
NSLog(#"%#",color);
}
Just a couple things I noticed in your code, your property should contain the specified protocol:
#property (retain) id <PassString> delegate;
And at some point in the class that's implementing the delegate methods, you have to assign the delegate to view controller 1. For example:
[viewController2Instance setPassingDelegate:self];

property not found of object of type 'segmentControl' iphone

I have defined segmentControl buttons.
#property(nonatomic, strong) SegmentControl *option;
SegmentControl *result = [[SegmentControl alloc] initWithFrame:frame items:#[#"Bad", #"Good"]];
result:self.option.selectedSegmentIndex
why am I getting this error: property 'selectedSegmentIndex' not found on object of type 'segmentControl',
#class SegmentControl;
#protocol SegmentControlDelegate <NSObject>
#optional
- (void)segmentControl:(SegmentControl *)segmentControl didSelectSegment:(UIButton *)segment;
#end
#interface SegmentControl : UIView
#property(nonatomic, assign) BOOL staySelected;
#property(nonatomic, weak) id<SegmentControlDelegate> delegate;
- (id)initWithFrame:(CGRect)frame items:(NSArray *)items;
#end
Your class SegmentControl needs to be a subclass of UISegmentControl (not UIView).

Overridden properties from superclass

I have a superclass of UIViewController - MasterViewController which declares a property called itemsViewController. This declares a method called from the MasterViewController, and is wired up via a storyboard in IB.
I have a subclass of MasterViewController which redeclares this property as a specific iPad version, but I can't access the redeclared property from the parent class.
MasterViewController
#interface MasterViewController : UIViewController {
}
#property (nonatomic, strong) IBOutlet ItemsViewController *itemsViewController;
#end
#implementation MasterViewController
#synthesize itemsViewController;
-(void)viewDidLoad {
// I can access itemsViewController in viewDidLoad.
}
#end
MasterViewController_iPad
#interface MasterViewController_iPad : MasterViewController {
IBOutlet ItemsViewController_iPad *_itemsViewController;
}
#property (nonatomic, strong) IBOutlet ItemsViewController_iPad *itemsViewController;
#end
#implementation MasterViewController_iPad
#synthesize itemsViewController = _itemsViewController;
-(void)viewDidLoad {
[super viewDidLoad];
// when I call super viewDidLoad, itemsViewController is nil, as though the property hasn't been overriden
// _itemsViewController is not nil in viewDidLoad.
}
#end
Am I misunderstanding the way property inheritance works in Objective-C?
You can't change the type signature of a method when you override a superclass method.
MasterViewController has these methods:
(void)setItemsViewController:(ItemsViewController *)foo
(ItemsViewController *)itemsViewController
But you're trying to give MasterViewController_iPad these methods:
(void)setItemsViewController:(ItemsViewController_iPad *)foo
(ItemsViewController_iPad *)itemsViewController
Which you can't do: you can't overload the same method name but have different types for the arguments.
If ItemsViewController_iPad is a subclass of ItemsViewController, a quick solution would be to keep the same signature as in MasterViewController but simply use an ItemsViewController_iPad when you set the property.
You can use category if you'd like to override property. Here is example:
I have PDFFileChooserViewController with PDFFileModel and PDFFilesDataSource and some logic related to this properties.
#class PDFFileModel, PDFFilesDataSource;
#interface PDFFileChooserViewController : UIViewController
#property (nonatomic, strong) PDFFileModel* selectedModel;
#property (nonatomic, strong) PDFFilesDataSource*dataSource;
#end
Then I'd like to add specific ViewController for choosing files from Dropbox but my model have some additional fields for example dropboxPath and my DropboxDataSource gets files using another way. So I decided to create category and override this properties:
#import "PDFFileChooserViewController.h"
#class DropboxFileModel,DropboxDataSource;
#interface DropboxViewController : PDFFileChooserViewController
#end
#interface DropboxViewController (ModelCategory)
#property(nonatomic, strong) DropboxFileModel* selectedModel;
#property(nonatomic, strong) DropboxDataSource* dataSource;
#end
Notice that this category will be visible inside DropboxViewController only where I can manipulate with that properties but another classes see only super class interface

How to use delegate methods to pass objects between two view controllers?

I'm using the Utility Application project to Xcode 4.2, then I am adding two text fields for each view: main view and the flipside view.
Now I wish to assign the value of the flipside text field to mainview text field.
MainViewController.h
#import "FlipsideViewController.h"
#interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
UITextField *nameField;
}
#property(nonatomic, retain) IBOutlet UITextField *nameField;
- (IBAction)showInfo:(id)sender;
#end
FlipsideViewController.h
#class FlipsideViewController;
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#end
#interface FlipsideViewController : UIViewController {
UITextField *changeText;
}
#property (nonatomic, retain) IBOutlet UITextField *changeText;
#property (assign, nonatomic) IBOutlet id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
FlipsideViewController.m
#import "FlipsideViewController.h"
#implementation FlipsideViewController
#synthesize delegate = _delegate;
#synthesize changeText;
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
When the done action starts, i want the changetext value be assigned to nameField text.
How do I do this?
In MainViewController.m, implement the flipSideViewControllerDelegate method as:
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
self.nameField.text=controller.changeText.text;
}
So when the done: method is called, this delegate method is also called with your flipSideViewController object as the argument, through which changeText can be accessed.
EDIT to answer question in comment:
In your protocol FlipSideViewControllerDelegate, add this method:
- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
Then it is similar to the other delegate method implementation in MainViewController.m
, which is how the protocol works really. If your MainViewController conforms to the protocol, it can implement methods of that protocol. By default all the methods declared in the protocol are optional, but you have the option to specify if the method is optional or required by using
#optional
//list of methods
#required
//list of methods
Bear in mind that if your a method is declared as required in the protocol, any class conforming to that protocol must implement it. Anyway, in your MainViewController.m:
- (void)flipsideViewControllerDidSelect:(NSIndexPath*)indexPath
{
int anInt=indexPath.row;
self.nameField.text=[NSString stringWithFormat:#"%d",anInt];
}

iphone make personal delegate

I have this problem....
in my viewcontroller.h I defined my class like this:
myClass* iR;
and after:
#property (nonatomic,retain) IBOutlet myClass* iR;
into myClass.h I added this:
#protocol myClassDelegate <NSObject>
-(void) didLogon:(bool)isLogged;
#end
and after:
#property (nonatomic, assign) id<myClassDelegate> delegate;
now, into my class, in the connectionDidFinishLoading method ( I used a nsurlconnection to retrieve the login data I added this:
[self.delegate didLogon:true];
into myviewcontroller.h:
<myClassDelegate>
and into myviewcontroller.m:
-(void)didLogon:(bool)isLogged{
...
}
but the program go inside the self.delegate didLogon but into myviewcontroller.m didn't go... did you understand why???
Where are you assigning the delegate? You need something like this:
MyViewController *viewController = [[MyViewController alloc] init];
self.delegate = viewController;
Just to be safe, when you call delegate methods, call them like this:
if ([self.delegate respondsToSelector:#selector(didLogon:)]) {
[self.delegate didLogon:YES];
}
That way, if the delegate doesn't support that method, your program won't crash when it doesn't recognize that selector.