custom viewcontroller delegate not working - iphone

i'm having issues on my custom delegate, not sure why is not working.i wan to pass the delegate to the mainview so it will auto push to a detail view after adding a new record.
Not sure what i have missed out.i'm doing all the linkage with storyboard & iOS 5.
thanks for looking and appreciated all comments
addnote.h
#import <UIKit/UIKit.h>
#import "memoView.h"
#protocol addNoteDelegate;
#interface addNote : UIViewController
{
IBOutlet UITextField *memoNameTextField;
IBOutlet UITextField *memoCommentsTextField;
id <addNoteDelegate> delegate;
}
#property (nonatomic,assign) id <addNoteDelegate> delegate;
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
#end
#protocol addNoteDelegate <NSObject>
-(void)addNoteDidFinish:(addNote *)controller;
-(void)addNoteDidCancel:(addNote *)controller;
#end
addnote.m
-(IBAction)add:(id)sender
{
[[self delegate] addNoteDidFinish:self];
}
mainview.h
#import <UIKit/UIKit.h>
#import "addNote.h"
#interface mainView : UITableViewController<NSFetchedResultsControllerDelegate, addNoteDelegate>
{
}
#property (nonatomic, retain) NSArray *memoInfo;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) NSManagedObjectContext *ObjectContext;
-(IBAction)backToMain:(id)sender;
- (IBAction)addNote:(id)sender;
#end
mainView.m
-(IBAction)addNote:(id)sender
{
addNote *addingNote = [[addNote alloc] init];
addingNote.delegate = self;
[self performSegueWithIdentifier:#"addNote" sender:self];
}
- (void)addNoteDidFinish:(addNote *)controller{
//PUSH TO NEXT VIEW
[[self navigationController] dismissModalViewControllerAnimated:YES];
}

#protocol addNoteDelegate <NSObject>
-(void)addNoteDidFinish:(addNote *)controller;
-(void)addNoteDidCancel:(addNote *)controller;
#end
#interface addNote : UIViewController
{
IBOutlet UITextField *memoNameTextField;
IBOutlet UITextField *memoCommentsTextField;
id <addNoteDelegate> delegate;
}
#property (nonatomic,assign) id <addNoteDelegate> delegate;
- (IBAction)done:(id)sender;
- (IBAction)cancel:(id)sender;
#end
and:
[self dismissModalViewControllerAnimated:YES];
not:
[[self navigationController] dismissModalViewControllerAnimated:YES];

Seems I was missing a function:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"addNote"]) {
UINavigationController *navController = [segue destinationViewController];
addNote *addingNote = (addNote *)navController.topViewController;
addingNote.delegate = self;
}
}

Related

Sharing Data between XIB's

I'm following the James Brannan tutorial's, and im trying to share data between some xbis. No luck.
I have 2 xib's. The first, simple button and textfield. The second, just a label, to show the result of the first xib textfield.
So, i dont know what im doing wrong. Im using NSObject like in tutorial.
SharedData.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface SharedData : NSObject {
NSString *MeuNome;
}
#property (nonatomic, retain) NSString *MeuNome;
#end
SharedData.m
#import "SharedData.h"
#implementation SharedData
#synthesize MeuNome;
- (void) dealloc {
self.MeuNome = nil;
[super dealloc];
}
#end
FirstStepViewController.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "SharedData.h"
#interface FirstStepViewController : UIViewController {
IBOutlet SharedData *sharedData;
IBOutlet UITextField *campoNome;
}
#property (nonatomic, retain) UITextField * campoNome;
#property (nonatomic, retain) SharedData *sharedData;
- (IBAction) takeNextStep: (id) sender;
#end
FirstStepViewController.m
#import "FirstStepViewController.h"
#import "SecondStepViewController.h"
#import "LuconeAppDelegate.h"
#implementation FirstStepViewController
#synthesize campoNome, sharedData;
- (IBAction) takeNextStep : (id) sender{
// declaracao de shared data
[sender resignFirstResponder];
self.sharedData.MeuNome = self.campoNome.text;
// faz animacao para proximo slide
SecondStepViewController *varSecondViewController = [[SecondStepViewController
alloc] initWithNibName:#"SecondStepViewController" bundle:nil ];
[self.navigationController pushViewController:varSecondViewController
animated: YES];
[self navigationController].navigationBarHidden = NO;
}
- (void)viewDidLoad {
[self navigationController].navigationBarHidden = YES;
[super viewDidLoad];
}
- (void)dealloc {
self.sharedData = nil;
//self.campoNome = nil;
[super dealloc];
}
#end
SecondStepViewController.h
#import <UIKit/UIKit.h>
#import "SharedData.h"
#interface SecondStepViewController : UIViewController {
IBOutlet SharedData *sharedData;
IBOutlet UILabel *nome;
}
#property (nonatomic, retain) SharedData *sharedData;
#property (nonatomic, retain) UILabel *nome;
#end
SecondStepViewController.m
#import "SecondStepViewController.h"
#import "SharedData.h"
#implementation SecondStepViewController
#synthesize nome, sharedData;
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = #"step two";
self.nome.text = self.sharedData.MeuNome;
}
- (void)dealloc {
self.sharedData = nil;
[super dealloc];
}
#end
What's wrong?
Thanks!
There are three small problems with your code.
IBOutlet is only for controls that you put on your view using interface builder e.g. UIButtons, UILabels. So it is unnecessary for instances of SharedData to be IBOutlets. Furthermore if you create your interface programmatically then using IBOutlet is again unnecessary.
You Declare that sharedData is an instance of SharedData class but you do not instantiate it. in your FirstStepViewController.m before you set any of sharedData's properties you should add the following code:
sharedData = [[sharedData alloc] init];
after that you can do:
self.sharedData.MeuNome = self.campoNome.text;
if you omit the "self." the code should work just as fine.
Finally before pushing the second view controller to the navigation stack you have to assign the sharedData object in your first view controller to sharedData in your second view controller.
in your FirstStepViewController.m add:
[varSecondViewController sharedData] = [self sharedData];
before:
[self.navigationController pushViewController:varSecondViewController
animated: YES];
Finally make sure you have connected all your outlets correctly in interface builder and everything should run perfectly then :)

Class Delegate does not implement protocol

I`m doing something wrong here but I can't figure out what it is .
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property (nonatomic, retain) IBOutlet UIPageControl *pageControl;
#property (nonatomic, retain) IBOutlet UIView *flipside;
#property (nonatomic, retain) NSMutableArray *viewControllers;
- (IBAction)showInfo:(id)sender;
- (IBAction)changePage:(id)sender;
#end
AppDelegate.m
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
This is where I`m getting :
warning: class 'AppDelegate' does not implement the 'FlipsideViewControllerDelegate' protocol.
After line :
controller.delegate = self;
My FlipsideViewController.h looks like this :
#import <UIKit/UIKit.h>
#protocol FlipsideViewControllerDelegate;
#interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
}
#property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
#end
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
#en
Any help would be greatly appreciated :)
It's exactly what the error message says. AppDelegate just doesn't implement the protocol. In your header file, add FlipsideViewControllerDelegate between the brackets (i.e. <UIApplicationDelegate, UIScrollViewDelegate, FlipsideViewControllerDelegate>), and implement the -flipsideViewControllerDidFinish: method.
try adding FlipsideViewControllerDelegate to the appDelegate
#interface AppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate,FlipsideViewControllerDelegate> {
UIWindow *window;
UIScrollView *scrollView;
UIPageControl *pageControl;
NSMutableArray *viewControllers;
UIView *flipside;
// To be used when scrolls originate from the UIPageControl
BOOL pageControlUsed;
}

change a label from another view

I have a label on my main View. I have a button which brings me to another view. On it I have a button to change my label which is on the main view.
How can I do this? I included a projet, could someone help me please...
I usually use a delegate protocol:
in the second view h add :
#protocol viewControllerDelegate;
#interface viewController : UIView
id < viewControllerDelegate > delegate;
#property (nonatomic, assign) id < QuickViewControllerDelegate > delegate;
#end
#protocol viewControllerDelegate
- (void)viewController:(ViewController *)controller stringForLabel:(NSString*)string;
#end
in the second view m file call:
[delegate quickViewController:self
stringForLabel:#"your string"];
in the main view h file add: , like that:
#interface MainView:UIViewController<viewControllerDelegate>
in the main view m file:
first: when you init the second view don't forget to add:
secondview.delegate = self;
other wise it wont work.
second: add the delegate function:
- (void)viewController:(ViewController*)controller stringForLabel:(NSString*)string{
//set the label from the string passed
lable.text = string;
}
hope it will help
shani
O.K i usually don't do that but for now...
this are your files after i have change them -
View1 .h (try to name those files with a capital letter);
#import <UIKit/UIKit.h>
#protocol View1Delegate;
#interface View1 : UIViewController {
id <View1Delegate> delegate;
IBOutlet UIButton *btn_changelbl;
IBOutlet UIButton *btn_back;
}
#property (nonatomic, assign) id <View1Delegate> delegate;
#property (nonatomic,retain) IBOutlet UIButton *btn_changelbl;
#property (nonatomic,retain) IBOutlet UIButton *btn_back;
-(IBAction) backToMain:(id)sender;
-(IBAction) changeLabel:(id)sender;
#end
#protocol View1Delegate
- (void)view1:(View1*)controller labelNeedsChage:(BOOL)needsChange stringForLabel:(NSString*)string;
#end
View1.m
#import "View1.h"
#import "testViewController.h"
#implementation View1
#synthesize delegate;
#synthesize btn_changelbl;
#synthesize btn_back;
-(IBAction) backToMain:(id)sender {
[self dismissModalViewControllerAnimated:YES];
[delegate view1:self labelNeedsChage:NO stringForLabel:nil];
}
-(IBAction) changeLabel:(id)sender{
[delegate view1:self labelNeedsChage:YES stringForLabel:#"new text"];
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[btn_changelbl release];
[btn_back release];
[super dealloc];
}
#end
TestViewController.h
#import <UIKit/UIKit.h>
#import "View1.h"
#interface testViewController : UIViewController<View1Delegate> {
IBOutlet UILabel *lb_test;
IBOutlet UIButton *btn_changeView;
}
#property (nonatomic,retain) IBOutlet UILabel *lb_test;
#property (nonatomic,retain) IBOutlet UIButton *btn_changeView;
-(IBAction) changeView:(id)sender;
#end
TestViewController.m
#import "testViewController.h"
#import "view1.h"
#implementation testViewController
#synthesize lb_test;
#synthesize btn_changeView;
-(IBAction) changeView:(id)sender {
View1 *myView = [[View1 alloc] init];
myView.delegate=self;
[self presentModalViewController:myView animated:YES];
}
- (void)view1:(View1*)controller labelNeedsChage:(BOOL)needsChange stringForLabel:(NSString*)string{
if(needsChange){
lb_test.text=string;
}
}
- (void)dealloc {
[lb_test release];
[btn_changeView release];
[super dealloc];
}
#end
Thats it.
by the way:
you had many small mistakes that i have fixed. look at the fixes to try to understand them.

Implementing delegate methods for modal view controller data transfer

I have a simple project to present a modal view controller and transfer back a string based on which button in the modal VC that gets pressed. I based it all on watching the Stanford class on iTunes U. It looks like I have everything correct, but I get a couple of compiler warnings.
First I get one called passing argument 1 of 'setDelegate:' from incompatible pointer type in TransferViewController.m
Second I get four warnings called Invalid receiver type 'id <MyModalViewControllerDelegate>*' but these aren't displayed in the build results area, rather next to the offending lines in MyModalViewController.m, both lines in each of the button actions.
Here's the code...
// TransferViewController.h
#import <UIKit/UIKit.h>
#import "MyModalViewController.h";
#interface TransferViewController : UIViewController <MyModalViewControllerDelegate> {
UILabel *label;
UIButton *button;
}
#property (nonatomic, retain) IBOutlet UILabel *label;
#property (nonatomic, retain) UIButton *button;
- (IBAction)updateText;
#end
// TransferViewController.m
#import "TransferViewController.h"
#implementation TransferViewController
#synthesize label;
#synthesize button;
- (IBAction)updateText {
MyModalViewController *myModalViewController = [[MyModalViewController alloc] init];
myModalViewController.delegate = self; // I get the warning here.
[self presentModalViewController:myModalViewController animated:YES];
[myModalViewController release];
}
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog {
label.text = selectedDog;
[self dismissModalViewControllerAnimated:YES];
}
#end
// MyModalViewController.h
#import <UIKit/UIKit.h>
#protocol MyModalViewControllerDelegate;
#interface MyModalViewController : UIViewController {
UIButton *abby;
UIButton *zion;
id <MyModalViewControllerDelegate> delegate;
}
#property (assign) id <MyModalViewControllerDelegate> delegate;
- (IBAction)selectedAbby;
- (IBAction)selectedZion;
#end
#protocol MyModalViewControllerDelegate <NSObject>
#optional
- (void)myModalViewController:(MyModalViewController *)controller didFinishSelecting:(NSString *)selectedDog;
#end
// MyModalViewController.m
#import "MyModalViewController.h"
#implementation MyModalViewController
#synthesize delegate;
- (IBAction)selectedAbby {
if ([self.delegate respondsToSelector:#selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:#"Abby"];
}
}
- (IBAction)selectedZion {
if ([self.delegate respondsToSelector:#selector (myModalViewController:didFinishSelecting:)]) {
[self.delegate myModalViewController:self didFinishSelecting:#"Zion"];
}
}
Get rid of those *s after id <something> and before delegate.
So make this
id <MyModalViewControllerDelegate> *delegate;
this
id <MyModalViewControllerDelegate> delegate;

how to pass a string value from one controller to another

I have a login controller ,and after the successful login i want to pass some string value to the menu page.however it does not work.the application crashes.
I have tried possible suggesstion of Ihuk and SAM from the link below
how to pass a string value from one view controller to another view controller
loginController.h:
#import <UIKit/UIKit.h>
#class RootViewController;
#class Menu;
#interface LoginController : UIViewController {
UIButton *login_Button;
UITextField *username_TextField;
UITextField *password_TextField;
RootViewController *mc1;
UINavigationController *navigationController;
Menu *mv1;
}
#property(nonatomic,retain) IBOutlet UIButton *login_Button;
#property(nonatomic,retain) IBOutlet UITextField *username_TextField;
#property(nonatomic,retain) IBOutlet UITextField *password_TextField;
#property(nonatomic,retain) RootViewController *mc1;
#property (nonatomic, retain) IBOutlet
UINavigationController *navigationController;
#property(nonatomic,retain)Menu *mv1;
- (IBAction)Login_Method:(id)sender;
-(id)initWithUserName:(NSString *)name ;
#end
loginController.m
#import "LoginController.h"
#import "Menu.h"
#import "ViewController.h"
#import "RootViewController.h"
#implementation LoginController
#synthesize mc1,mv1;
#synthesize login_Button,username_TextField,password_TextField;
#synthesize navigationController;
// Implement viewDidLoad to do additional setup after
// loading the view, typically from a nib.
- (void)viewDidLoad {
if (![self.navigationController isNavigationBarHidden])
[self.navigationController setNavigationBarHidden:YES animated:NO];
//[self presentModalViewController:navigationController animated:YES];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)Login_Method:(id)sender
{
Menu *mv2 = [[Menu alloc] initWithUserName:#"Menu" bundle:nil];
//mv2.l1.text=#"aa"; //i tried this, but not work,so created initWithUserName
self.mv1=mv2;
[self presentModalViewController:mv1 animated:YES];
// [RootViewController release];
}
-(id)initWithUserName:(NSString *)name
{
self = [super init];
if (nil == self) {
return nil;
}
// display or store login info somewhere
[mv1.l1 setText:name];
return self;
}
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
- (void)dealloc {
[username_TextField release];
[password_TextField release];
[super dealloc];
}
#end
Menu.h
#import <UIKit/UIKit.h>
#class Menu;
#interface Menu : UIViewController {
UILabel *l1;
UIButton *AccountSummary_Button;
UIButton *PayOffQuote_Button;
UIButton *PayBill_Button;
UIButton *Logout_Button;
UINavigationController *nv1;
}
#property(nonatomic,retain) IBOutlet UILabel *l1;
#property(nonatomic,retain) IBOutlet UIButton *AccountSummary_Button;
#property(nonatomic,retain) IBOutlet UIButton *PayOffQuote_Button;
#property(nonatomic,retain) IBOutlet UIButton *PayBill_Button;
#property(nonatomic,retain) IBOutlet UIButton *Logout_Button;
#property (nonatomic, retain) IBOutlet UINavigationController *nv1;
-(IBAction)ViewAccountSummary_method:(id)sender;
-(IBAction)ViewPayOffQuote_method:(id)sender;
-(IBAction)ViewPayBill_method:(id)sender;
-(IBAction)Logout_method:(id)sender;
#end
Menu.m
Make a property in your application delegate, for example, an NSString* called myString.
Then access it from the login controller and other controllers like so:
[[UIApplication sharedApplication] delegate].myString
You could, for example, set myString's value in the login controller:
[[UIApplication sharedApplication] delegate].myString = #"value";
You could read it in any other controller:
NSLog(#"myString is: %#", [[UIApplication sharedApplication] delegate].myString);
Additionally, some Cocoa style tips:
Do not capitalize class members (AccountSummary_Button etc.)
Do not capitalize class methods (ViewAccountSummary_method etc.)
The only thing you should capitalize are the classes themselves (Menu etc.).