Pass data back from tableview to viewcontroller textfield Objective - C - ios

I have a UIViewController and a UITableViewController.
In ViewController I have a UIButton and UITextfield, when I press button it will navigate to TableViewController with static data in the tableview like (iPhone 5S, iPhone 6, iPhone 6S, iPhone 7), when I select any of the row it should be displayed in the textfield of ViewController. Tried but cannot get this one. Help me to solve this. below is my code:
ViewController.h
————————————————
#import <UIKit/UIKit.h>
#import "Utility.h"
#interface ViewController : UIViewController
- (IBAction)oneButtonClicked:(id)sender;
#property (strong, nonatomic) IBOutlet UITextField *txtOne;
ViewController.m
————————————————
//Can't understand what to do in this viewcontroller.
TableViewController.h
—————————————————————-
#import <UIKit/UIKit.h>
#import "Utility.h"
#interface FamilyDetailsViewController : UITableViewController
#end
TableViewController.m
—————————————————————-
#import "FamilyDetailsViewController.h"
#interface FamilyDetailsViewController ()
{
NSArray *arr;
}
#end
#implementation FamilyDetailsViewController
- (void)viewDidLoad {
[super viewDidLoad];
arr=[[NSArray alloc]initWithObjects:#"Parent",#"Grandparent",#"Sibling",#"Child",#"Other", nil];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [arr count];
}
- (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];
}
cell.textLabel.text=[arr objectAtIndex:indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.navigationController popViewControllerAnimated:YES];
}

in `ViewControllerB.h` which is a TableViewController declare a delegate to pass the data to ViewControllerA and create a delegate object
#import <UIKit/UIKit.h>
#protocol DataDelegate <NSObject>
- (void)passData:(NSString *)data;
#end
#interface ViewControllerB : UIViewController
#property (weak, nonatomic) id<DataDelegate> delegate;
#end
in ViewControllerB.m file
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedText = titleArray[indexPath.row];
if [self.delegate respondsToSelector:#selector(passData:)]) {
[self.delegate passData:"hello"];
}
[self.navigationController popViewControllerAnimated:YES];
}
**in ViewControllerA**
#import "ViewControllerA.h"
#import "ViewControllerB.h"
#interface ViewControllerA () <DataDelegate>
#property (strong, nonatomic) ViewControllerB *viewControllerB;
#end
#implementation ViewControllerA
- (void)viewDidLoad {
_viewControllerB.delegate = self;
}
- (void)passData:(NSString *)data {
self.textField.text = data
}

Try Using NSNotification. Pass data back to ViewController with NSNotification. First register a notification in viewWillAppear of ViewController.
Try below code:
In ViewController:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(getTextFromTable:) name:#"PassTextBackToViewController" object:nil];
}
Then define Method in ViewController like this:
-(void)getTextFromTable:(NSNotification *)notification
{
[self.textField setText:[notification.userInfo valueForKey:#"cellText"]];
}
And in tableView's didSelectRowAtIndexPath method, Post the notification:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text,#"cellText", nil]
[[NSNotificationCenter defaultCenter]postNotificationName:#"PassTextBackToViewController" object:dictionary];
[self.navigationController popViewControllerAnimated:YES];
}
Make Sure, Notification's name must be same in both ViewController and
TableViewController

Follow this:
In AppDelegate.h
#property (strong, nonatomic) NSString *selectedText;
In ViewController.m
-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
txtOne.text = appDelegate.selectedText;
}
Make appDelegate.selectedText = #"" while navigating to UITableViewController Class.
In FamilyDetailsViewController.m
pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selected= [arr objectAtIndex:indexPath.row];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.selectedText = selected;
[self.navigationController popViewControllerAnimated:YES];
}
In this way you are storing selected value in Singleton class.(App delegate). You can fetch data from this in ViewController(viewWillAppear).

Related

How to pass data from 2nd viewcontroller to tableviewcontroller using delegates

I have 2 viewcontrollers - one is a tableview and the other is a normal viewcontroller. I want pass data from the second viewcontroller to a tableview controller by using delegates. I have created a delegate and delegatemethod in viewcontroller and implemented delegatemethod in the tableview controller. The problem is that I am getting data to the array but tableview is not reloading. Why?
Can anyone help with this problem? Thanks in advance.
#import "TableViewController.h"
#interface TableViewController ()<name>{
NSMutableArray *data;
}
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
data = [NSMutableArray array];
[self.tableView reloadData];
}
- (IBAction)callingSecondView:(id)sender {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ViewController *var = [storyboard instantiateViewControllerWithIdentifier:#"vc"];
var.delegate = self;
[self.navigationController pushViewController:var animated:YES];
}
-(void)getdata:(NSString *)name{
[data addObject:name];
[self.tableView reloadData];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
if (cell != nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
And I am creating delegate a object and protocol in ViewController.h
#import <UIKit/UIKit.h>
#protocol name <NSObject>
-(void)getdata : (NSString *)name;
#end
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *txt;
- (IBAction)done:(id)sender;
#property(nonatomic,retain) id<name> delegate;
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)done:(id)sender {
[delegate getdata:self.txt.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
#end
I think the first VC don't reloadData because it's not the visible VC. Try reloadData when the VC willAppear.
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}

TableView data pass mistake

I'm following a textbook and it's a bit outdated so I ended up mixing my code with it.
Its a basic app that populates a table view with Cities (london,san francisco,sydney and madrid) and upon clicking it, It should open up a detail view that should display the city as its title and an image of the city with the description, somehow even if I click any cell it shows me "London" with London's picture and description.
Here's my code
ViewController.m
#import "ViewController.h"
#import "City.h"
#import "AppDelegate.h"
#import "CityController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
citys = appDelegate.cities;
}
#pragma mark - UITableViewDataSource Methods
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
City *theCity = [citys objectAtIndex:indexPath.row];
cell.textLabel.text = theCity.cityName;
return cell;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [citys count];
}
#pragma mark - UITableViewDelegate Methods
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
CityController.m (the detail View)
#import "CityController.h"
#import "AppDelegate.h"
#import "City.h"
#implementation CityController
- (id) initWithIndexPath:(NSIndexPath *)indexPath{
if((self = [super init])){
index = indexPath;
}
return self;
}
- (void) viewDidLoad{
[super viewDidLoad];
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
City *thisCity = [delegate.cities objectAtIndex:index.row];
self.title = thisCity.cityName;
labelDescription.text = thisCity.cityDescription;
//descriptionView.editable = NO;
pictureView.image = thisCity.cityImage;
}
#end
City.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface City : NSObject
#property (nonatomic, strong) NSString *cityName;
#property (nonatomic, strong) NSString *cityDescription;
#property (nonatomic, strong) UIImage *cityImage;
#end
You don't show in your code how you are calling this method on your CityController
- (id) initWithIndexPath:(NSIndexPath *)indexPath
Therefore indexPath.row in CityController will always be zero which I'm assuming is the first entry in the cities that relates to London
You need to do something in this method to set the indexPath correctly for the CityController
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Are you using Storyboards?
Your code must be,
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CityController *cityVC = [[CityController alloc] initWithIndexPath:indexPath];
[[self view] addSubview:[cityVC view]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

iOS Use of undeclared identifier in protocol?

I have two view controllers, HomeViewController (hereafter HVC) and AddActivityViewController (hereafter AAVC). In AAVC, I've declared a delegate protocol:
#protocol AddActivityViewControllerDelegate;
and defined it thusly:
#protocol AddActivityViewControllerDelegate
-(void) addActivityViewControllerDidSave;
-(void) addActivityViewControllerDidCancel:(ListActivity *) activityToDelete;
#end
Next, I implemented the two methods in HVC (the delegate) like this:
-(void) addActivityViewControllerDidSave
{
[self.moc MR_saveToPersistentStoreAndWait];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void) addActivityViewControllerDidCancel:(ListActivity *) activityToDelete
{
[activityToDelete MR_deleteEntity];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
I get this error "Use of undeclared identifier 'addActivityViewControllerDidSave' even though it's clearly declared in the protocol.
I should mention that before this, I was dealing with what was apparently an "import loop," which caused an "undeclared protocol" error. That error seems to have been fixed.
Here are the #import statements from the HomeViewController.h file:
#import <UIKit/UIKit.h>
#import "ListActivity.h"
#import "AddActivityViewController.h"
#import "TimedActivity.h"
#interface HomeViewController : UIViewController <AddActivityViewControllerDelegate>
#property (strong, nonatomic) IBOutlet UITableView *myTableView;
#property NSManagedObjectContext * moc;
- (IBAction)dumpMemory:(UIButton *)sender;
#end
And from the AddActivityViewController.h file:
#import <UIKit/UIKit.h>
#import "ListActivity.h"
#protocol AddActivityViewControllerDelegate;
#interface AddActivityViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *activityField;
#property (weak, nonatomic) IBOutlet UITextField *categoryField;
#property (strong, nonatomic) ListActivity *thisActivity;
#property (nonatomic, weak) id <AddActivityViewControllerDelegate> delegate;
- (IBAction)saveButton:(UIBarButtonItem *)sender;
- (IBAction)cancelButton:(UIBarButtonItem *)sender;
#end
#protocol AddActivityViewControllerDelegate
-(void) addActivityViewControllerDidSave;
-(void) addActivityViewControllerDidCancel:(ListActivity *) activityToDelete;
#end
I can post the full content of all four class files if that is helpful.
Many thanks for helping!
Edit: Here's the full code from HomeViewController.m:
//
// HomeViewController.m
// MRExample
//
// Created by Tim Jones on 1/15/14.
// Copyright (c) 2014 TDJ. All rights reserved.
//
#import "HomeViewController.h"
#import "ListActivity.h"
#interface HomeViewController ()
{
NSFetchedResultsController *frc;
}
#end
#implementation HomeViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.automaticallyAdjustsScrollViewInsets = NO;
[self refreshData];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationNewActivityAdded:) name:#"newActivityAdded" object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) notificationNewActivityAdded:(NSNotification*)notification
{
[self refreshData];
}
#pragma mark Table View data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[frc sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
// Customize the appearance of table view cells.
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
// Configure the cell to show the activity's name
ListActivity *thisActivity = [frc objectAtIndexPath:indexPath];
cell.textLabel.text = thisActivity.activityName;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
cell.textLabel.textColor = [UIColor redColor];
NSAttributedString *attString;
attString = cell.textLabel.attributedText;
return cell;
}
// Section Label
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionLabel = [[[frc sections] objectAtIndex:section]name];
return [sectionLabel uppercaseString];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
ListActivity *thisActivity = [frc objectAtIndexPath:indexPath];
TimedActivity *currentActivity = [TimedActivity MR_createInContext:localContext];
currentActivity.timedActivityName = thisActivity.activityName;
currentActivity.category = thisActivity.activityCategory;
currentActivity.timedActivityTapped = [NSDate date];
[localContext MR_saveToPersistentStoreAndWait];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
ListActivity *activityToTrash = [frc objectAtIndexPath:indexPath];
// Delete the row from the data source
[activityToTrash MR_deleteEntity];
[localContext MR_saveToPersistentStoreAndWait];
[self refreshData];
}
}
-(void) refreshData
{
//This was the turning point for proper MR grouping. The two Properties (activityCategory and activityName) are used as Sort descriptors in the underlying core data methods
frc = [ListActivity MR_fetchAllSortedBy:#"activityCategory,activityName" ascending:YES withPredicate:nil groupBy:#"activityCategory" delegate:nil];
[self.myTableView reloadData];
}
- (IBAction)dumpMemory:(UIButton *)sender
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[ListActivity MR_truncateAllInContext:localContext];
[localContext MR_saveToPersistentStoreAndWait];
[self refreshData];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSManagedObjectContext *localContext = [[NSManagedObjectContext alloc] init];
if ([[segue identifier] isEqualToString:#"addActivity"])
{
AddActivityViewController *aavc = (AddActivityViewController *) [segue destinationViewController];
aavc.delegate = self;
ListActivity *newActivity = [ListActivity MR_createInContext:localContext];
aavc.thisActivity = newActivity;
}
-(void) addActivityViewControllerDidSave
{
[self.moc MR_saveToPersistentStoreAndWait];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void) addActivityViewControllerDidCancel:(ListActivity *) activityToDelete
{
[activityToDelete MR_deleteEntity];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
}
#end
Okay, just as I thought. The problem is that you're missing a bracket "}" at the end of the - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. Also you have an extra bracket at the end of the class (right above the #end keyword). That's probably the missing bracket. Fix that and your problem will go away.
Hope this helps!
I don't know if it's the problem, but move the declaration of your delegate (with the #class directive) to the top of AddActivityViewController, like this:
#import <UIKit/UIKit.h>
#import "ListActivity.h"
#class AddActivityViewController;
#protocol AddActivityViewControllerDelegate
- (void)addActivityViewControllerDidSave;
- (void)addActivityViewControllerDidCancel:(ListActivity *) activityToDelete;
#end
#interface AddActivityViewController : UIViewController
#property (nonatomic, weak) id <AddActivityViewControllerDelegate> delegate;
#property (nonatomic, weak) IBOutlet UITextField *activityField;
#property (nonatomic, weak) IBOutlet UITextField *categoryField;
#property (nonatomic, strong) ListActivity *thisActivity;
- (IBAction)saveButton:(UIBarButtonItem *)sender;
- (IBAction)cancelButton:(UIBarButtonItem *)sender;
#end

How to enable a UI botton in ViewController A upon on row selection in TableViewController B. Both View controllers are nested in TabarController

I have two view controllers in tab bar controller. ViewControllerA has two buttons (MybuttonA and MybuttonB with enabled box unchecked in storyboard). ViewControllerB is a TableViewController. I would like to enable buttons in ViewControllerA upon selecting specific rows in ViewControllerB table.
Greatly appreciate any help...
ViewControllerA.h
#import <UIKit/UIKit.h>
#interface ViewControllerA : UIViewController {
IBOutlet UIButton * MybuttonA;
IBOutlet UIButton * MybuttonB;
}
-(IBAction)mybuttonaction:(id)sender;
#property(strong,nonatomic)UIButton *MybuttonA;
#property(strong,nonatomic)UIButton *MybuttonB;
#end
ViewControllerA.m
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
#synthesize MybuttonA;
#synthesize MybuttonB;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(IBAction)mybuttonaction:(id)sender{
NSString * link = #"https://google.com";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:link]];
}
ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
#interface ViewControllerB : UITableViewController{
ViewControllerA *viewcontrollerA;
}
#property (nonatomic, retain) ViewControllerA *viewcontrollerA;
#end
ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
#interface ViewControllerB () {
}
#end
#implementation ViewControllerB
#synthesize viewcontrollerA;
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"CONTENTS";
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:#selector(reload) forControlEvents:UIControlEventValueChanged];
[self reload];
[self.refreshControl beginRefreshing];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
{return 5;}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat: #"Cell #%d", indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString* value = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
if ([value isEqual:#"1"]){
viewcontrollerA.MybuttonA.enabled=YES;
}
else if ([value isEqual:#"2"])
{
viewcontrollerA.MybuttonB.enabled=YES;
}
}
else {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
#end
In ViewControllerB, add the following line to viewDidLoad:
viewControllerA = [self.tabBarController.viewControllers objectAtIndex:0];
Then, the rest of your code should work as you have it.
Note that you should generally use self.viewControllerA instead of accessing the iVar directly unless you have a good reason for it. :-)

How to pass a value from one UIViewController to another

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
remove delegate methods.
import your second class to first one.
in 2nd class import first class and implement id firstClass variable there.
when you pushing 2nd class, set id from (3) to self.
when you'v done and ready to pass it, set firstClass.passedValue = passingValue
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;
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);

Resources