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

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

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

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

UIAlertViewDelegate extended protocol

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

Properly declare delegation in Objective C (iPhone)

Ok, This has been explained a few times (I got most of the way there using this post on SO), but I am missing something. I am able to compile cleanly, and able to set the delegate as well as call methods from the delegate, but I'm getting a warning on build:
No definition of protocol 'DetailViewControllerDelegate' is found
I have a DetailViewController and a RootViewController only. I am calling a method in RootViewController from DetailViewController. I have the delegate set up as so:
In RootViewController.h:
#import "DetailViewController.h"
#interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate, DetailViewControllerDelegate> //Error shows up here
{
//Some Stuff Here
}
//Some other stuff here
#end
In RootViewController.m I define the delegate when I create the view using detailViewController.delegate = self
In DetailViewController.h:
#protocol DetailViewControllerDelegate;
#import "RootViewController.h"
#interface DetailViewController : UITableViewController <UITextFieldDelegate>
{
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
#protocol DetailViewControllerDelegate
//some methods that reside in RootViewController.m
#end
I feel weird about declaring the protocol above the import in DetailViewController.h, but if I don't it doesn't build. Like I said, the methods are called fine, and there are no other errors going on. What am I missing here?
pheelicks is pretty much there but it looks like some of your protocol methods also use the DetailViewController class, I imagine it looks something like this :
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
#end
#class DetailViewController : UITableViewController <UITextFieldDelegate> {
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
and you haven't defined DetailViewController yet so you will get an error in the protocol definition.
You can fix this in two ways :
a) Declare (but don't define yet) the class before the protocol
#class DetailViewController;
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
#end
b) Just use UITableViewController instead of DetailViewController in your protocol methods.
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(UITableViewController *)controller hasSomething:(id)thing;
#end
Personally, I choose solution (a) but it really depends on what you're trying to do.
Hope that helps.
Try:
In DetailViewController.h:
#import "RootViewController.h"
#protocol DetailViewControllerDelegate <NSObject>
//some methods that reside in RootViewController.m
#end
#interface DetailViewController : UITableViewController <UITextFieldDelegate>
{
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
Here is another way that you could tackle this, similar to the solution proposed by deanWombourne.
#protocol DetailViewControllerDelegate;
#interface DetailViewController : UITableViewController <UITextFieldDelegate> {
id <DetailViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <DetailViewControllerDelegate> delegate;
#end
#protocol DetailViewControllerDelegate <NSObject>
- (void) controller:(DetailViewController *)controller hasSomething:(id)thing;
#end

Syntax for creating View delegate in Objective-C

I am trying to create a delegate protocol for a custom UIView. Here is my first attempt:
#protocol FunViewDelegate
#optional
- (void) funViewDidInitialize:(FunView *)funView;
#end
#interface FunView : UIView {
#private
}
#property(nonatomic, assign) id<FunViewDelegate> delegate;
#end
This doesn't work because the FunView interface has not been declared at the time of the FunViewDelegate declaration. I have tried adding a prototype ala C++ before the #protocol:
#interface FunView;
But this just drives the compiler nuts. How am I supposed to do this?
Forward class syntax is #class Foo;, not #interface Foo;.
It would seem that you can forward declare protocols:
#protocol FunViewDelegate;
#interface FunView : UIView {
#private
id<FunViewDelegate> delegate;
}
#property(nonatomic, assign) id<FunViewDelegate> delegate;
#end
#protocol FunViewDelegate
#optional
- (void) funViewDidInitialize:(FunView *)funView;
#end