Passing an Object between two views

Discussion in 'iPhone/iPad Programming' started by GhostDZ9, May 24, 2011.

  1. macrumors regular

    Joined:
    Sep 13, 2010
    #1
    Hey guys,

    I was just wondering what is the best way to pass an object between two views and how would I go about doing so?

    Regards,

    Ghost
     
  2. macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    Use either delegates or implement a singleton class.
     
  3. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #3
    Code:
    //
    //  TimerViewController.h
    
    #import <UIKit/UIKit.h>
    #import "StaticClass.h"
    #import "TimeOptionController.h"
    
    
    
    @interface TimerViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    	NSMutableArray  *sections;
    	NSDateFormatter *formatter;
    	IBOutlet UITableView *tvc;
    	NSString *duration1;
    }
    
    @property (nonatomic, assign) NSDateFormatter *formatter;
    @property (retain) NSMutableArray *sections;
    @property (nonatomic, retain) UITableView *tvc;
    @property (nonatomic, retain) NSString *duration1;
    
    
    -(IBAction)startTimeBtnPressed;
    
    
    @end
    
    Code:
    //TimerViewController.m
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        }
        
    	if(indexPath.section == 0){
    		cell.textLabel.text = [sections objectAtIndex:indexPath.row];
    		cell.detailTextLabel.text = duration1; 
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	
    	return cell;
    }

    Code:
    //TimeOptionController.h
    
    #import <UIKit/UIKit.h>
    
    
    @interface TimeOptionController : UITableViewController {
    	NSArray *rows;
    	int selectedRow;
    	NSString *duration;
    
    }
    @property (retain) NSArray *rows;
    @property (nonatomic, retain) NSString *duration;
    @property (nonatomic, retain) NSString *duration1;
    
    
    @end
    


    Code:
    //TimeOptionController.m
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	selectedRow = indexPath.row;
    	duration = (NSString *) [rows objectAtIndex:indexPath.row];
    	duration1 = duration;
    	[self.tableView reloadData];
    
    }
    

    I wanna copy the value of duration from the TimeOptionController to the TimerViewController.


    SOMEONE PLEASE HELP ME!
     
  4. macrumors newbie

    Joined:
    May 25, 2011
    #4
    Does duration have to be an NSString?

    Im quite new to programming and i have only worked out how to pass int's floats's and things like that.

    jon
     
  5. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #5
    Yes it does because the value of it is filling in the DetailedTextLabel.text
     
  6. macrumors newbie

    Joined:
    May 25, 2011
    #6
  7. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #7
    I have read that article, if you can suggest another way to do what i am trying to do then please do so
     
  8. macrumors newbie

    Joined:
    May 25, 2011
    #8
    have you tried making it global?
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    The issue you are butting heads with is normally solved with something commonly called data persistence. There are a number of ways to code a solution, including singletons, using the app delegate (which is a singleton), NSUserDefaults, or even setting up a property in one class and setting its value in the other instance, among others. As well, there have been a number of threads in this forum that have previously covered this topic. Search for "data persistence" or "sharing values/variables between views".
     
  10. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #10
    Thanks Dejo, ill look into it further
     

Share This Page