Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 24, 2011, 12:20 PM   #1
GhostDZ9
macrumors regular
 
Join Date: Sep 2010
Passing an Object between two views

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
__________________
- 30 GB iPod Video - Black
- 13.3" MB - 4GB RAM - 500GB 7200RPM HDD
- 16 GB iPhone 4 - Black
- Game Center Tag: GhostDZ9
GhostDZ9 is offline   0 Reply With Quote
Old May 24, 2011, 01:21 PM   #2
dantastic
macrumors 6502
 
Join Date: Jan 2011
Use either delegates or implement a singleton class.
dantastic is offline   0 Reply With Quote
Old May 25, 2011, 08:58 AM   #3
GhostDZ9
Thread Starter
macrumors regular
 
Join Date: Sep 2010
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!
__________________
- 30 GB iPod Video - Black
- 13.3" MB - 4GB RAM - 500GB 7200RPM HDD
- 16 GB iPhone 4 - Black
- Game Center Tag: GhostDZ9
GhostDZ9 is offline   0 Reply With Quote
Old May 25, 2011, 09:12 AM   #4
jaybee2002
macrumors newbie
 
Join Date: May 2011
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
jaybee2002 is offline   0 Reply With Quote
Old May 25, 2011, 09:14 AM   #5
GhostDZ9
Thread Starter
macrumors regular
 
Join Date: Sep 2010
Yes it does because the value of it is filling in the DetailedTextLabel.text
__________________
- 30 GB iPod Video - Black
- 13.3" MB - 4GB RAM - 500GB 7200RPM HDD
- 16 GB iPhone 4 - Black
- Game Center Tag: GhostDZ9
GhostDZ9 is offline   0 Reply With Quote
Old May 25, 2011, 09:19 AM   #6
jaybee2002
macrumors newbie
 
Join Date: May 2011
yea well you cant use a global because with a nsstring you will have a world of memory leaks and what not u might wana go with what dantastic said and go with a singleton.

http://cocoawithlove.com/2008/11/sin...top-level.html

this is quite a good article to read about them.

jon
jaybee2002 is offline   0 Reply With Quote
Old May 25, 2011, 09:26 AM   #7
GhostDZ9
Thread Starter
macrumors regular
 
Join Date: Sep 2010
I have read that article, if you can suggest another way to do what i am trying to do then please do so
__________________
- 30 GB iPod Video - Black
- 13.3" MB - 4GB RAM - 500GB 7200RPM HDD
- 16 GB iPhone 4 - Black
- Game Center Tag: GhostDZ9
GhostDZ9 is offline   0 Reply With Quote
Old May 25, 2011, 09:30 AM   #8
jaybee2002
macrumors newbie
 
Join Date: May 2011
have you tried making it global?
jaybee2002 is offline   0 Reply With Quote
Old May 25, 2011, 09:35 AM   #9
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
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".
__________________
dejo is offline   0 Reply With Quote
Old May 25, 2011, 09:39 AM   #10
GhostDZ9
Thread Starter
macrumors regular
 
Join Date: Sep 2010
Thanks Dejo, ill look into it further
__________________
- 30 GB iPod Video - Black
- 13.3" MB - 4GB RAM - 500GB 7200RPM HDD
- 16 GB iPhone 4 - Black
- Game Center Tag: GhostDZ9
GhostDZ9 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
passing a value through an object to another object larswik Mac Programming 5 Sep 3, 2010 05:37 AM
how to pass the variable between two classes srinivas.be88 iPhone/iPad Programming 6 Aug 28, 2010 10:22 AM
Pass object between classes and copy of object donnib Mac Programming 8 Jun 27, 2010 03:14 PM
Passing data between two views?? azg442 iPhone/iPad Programming 2 May 11, 2009 12:10 PM
Passing Objects Between Views Lakario iPhone/iPad Programming 13 Oct 30, 2008 11:31 AM


All times are GMT -5. The time now is 05:50 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC