0

I am making an app where I have to pass a value from a second class to a first class. I have created a delegate method for that in second class.

In second class I have a UITextField, and if enter any text in this textfield it should be passed to a cell in a UITableView in first view.

However, in my case the value is not being passed properly. What have I done wrong?

This is my code:

second.h

#import <UIKit/UIKit.h>
@protocol secondDelegate<NSObject>
@required
- (void)setsecond:(NSString *)inputString;
@end

@interface second : UIViewController {
    IBOutlet UITextField *secondtextfield;
    id<secondDelegate>stringdelegate;
    NSString *favoriteColorString; 
}
@property (nonatomic, retain) UITextField *secondtextfield;
@property (nonatomic, assign) id<secondDelegate>stringdelegate;
@property (nonatomic, copy) NSString *favoriteColorString;
@end

second.m

#import "second.h"

@implementation second
@synthesize stringdelegate, secondtextfield, favoriteColorString;

- (void)viewWillDisappear:(BOOL)animated {
    [[self stringdelegate] setsecond:secondtextfield.text];
    favoriteColorString=secondtextfield.text;
    NSLog(@"thuis check:%@", favoriteColorString);
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //[[self stringdelegate] setsecond:secondtextfield.text];
    //favoriteColorString = secondtextfield.text;
    //NSLog(@"thuis check:%@", favoriteColorString);
}
@end    

first.h

#import <UIKit/UIKit.h>
#import "second.h"
#import "TextviewExampleAppDelegate.h"

@interface first : UITableViewController<secondDelegate> {
    //TextviewExampleAppDelegate *app;
    TextviewExampleAppDelegate *check;
}

first.m

@implementation first

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = @"message";
    cell.detailTextLabel.text = check.favoriteColorString;
    NSLog(@"this second check:%@", check.favoriteColorString);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    second *viewtwo = [[second alloc] initWithNibName:@"second" bundle:nil];
    //viewtwo.favoriteColorString = indexPath;
    viewtwo.stringdelegate = self;
    [self.navigationController pushViewController:viewtwo animated:YES];
    [viewtwo release];
}

- (void)setsecond:(NSString *)inputString { 
    if (nil != self.stringdelegate) {
        [self.stringdelegate setsecond:inputString];
    }
    [self.tableView reloadData];
}
@end
HAS
  • 19,140
  • 6
  • 31
  • 53
Rocky
  • 1,427
  • 1
  • 28
  • 65
  • why u have created object for TextviewExampleAppDelegate *check; create the object for second class in which you are creating the object. – Anil Kothari Dec 05 '11 at 08:31
  • @coolanikothari can u help me what i have to do try as par ur ans but i not any solution please help me out – Rocky Dec 05 '11 at 09:49

2 Answers2

4
  1. remove delegate methods.
  2. import your second class to first one.
  3. in 2nd class import first class and implement id firstClass variable there.
  4. when you pushing 2nd class, set id from (3) to self.
  5. when you'v done and ready to pass it, set firstClass.passedValue = passingValue
  6. pop second class

for example:

//first.h:
#import "second.h"
@class second

//second.h:
#import "first.h"
@class first
...
id firstClass;

//first.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    second *viewtwo =[[second alloc]initWithNibName:@"second" bundle:nil];
    [self.navigationController pushViewController:viewtwo animated:YES];
    viewtwo.firstClass = self;
    [viewtwo release];
}

//second.m:
firstClass.passedValue = self.passingValue;
SentineL
  • 4,682
  • 5
  • 19
  • 38
3

Please refer following rough scratch:

in application delegate .h

Create variable

NSString *varStr;

Assign Property

@propery (nonatomic, retain) NSString *valStr;

In delegate .m

@synthesize varStr;

initialize var

varStr = [NSString strinWithFormat:@"Hi"];

in First class

create delegate var;

delegate class *var = (delegate class*)[[UIApplication sharedApplication] delegate];

set value

var.varStr =  [NSString strinWithFormat:@"First"];

get value

NSLog (@"%@",var.varStr);

in Second class

create delegate var;

delegate class *var = (delegate class*)[[UIApplication sharedApplication] delegate];

set value

var.varStr =  [NSString strinWithFormat:@"Second"];

get value

NSLog (@"%@",var.varStr);
Community
  • 1
  • 1
Chetan Bhalara
  • 10,326
  • 6
  • 32
  • 51
  • `delegate class var = (delegate class)[[UIApplication sharedApplication] delegate];` is not working man!!!...what can i do?i wanted to try your method in my application – Emon Dec 13 '11 at 08:52
  • Actually i have done nothing....i just want to know how can i pass multiple string data from one controller to another.I have used your method but when i use `delegate class var = (delegate class)[[UIApplication sharedApplication] delegate];` then it shows delegate undefined.May be i have ask you a stupid question but actually i have never used `sharedApplication`.So help me please – Emon Dec 13 '11 at 09:11