Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-02-2012, 11:32 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default pass data from view to view and delegates..

hi there, I my program I need to pass MSMutableArrays from one to another viewcontrollers but I', not sure if the "basic scheme" of my program is ok..

I handle all conection voids and xmlparser in main controller.. is it good way?

shouldn't I handle this in AppDelegate.m file? I'm just unsure about if I doing it good (it is working but still..)

and another, for passing data I shouldn't use global variables so I should use delegate.. I make a simple example but it is not working

AppDelegate.h
Code:
#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>{


    NSMutableArray *pole;



}
@property (nonatomic, retain) NSMutableArray *pole;
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end
AppDelegate.m
Code:
@synthesize pole;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
   
    
    pole = [[NSMutableArray alloc] init];
    
    NSString * f = @"foo";
    NSString * b = @"bar";
    NSString * z = @"baz";
  pole = [NSMutableArray arrayWithObjects:f,b,z,nil];
    for (id obj in pole){
        NSLog(@"%@", obj);
    }
    
    return YES;

}
ViewController.m
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    NSMutableArray *temp = appDelegate.pole;
   // NSLog(@"%i",temp);
    for (id obj in temp){
        NSLog(@"%@", obj);
    }
}
so.. red ones are my added code.. in AppDelegate.m is code working - NSLOG is giving me the foo bar baz items but in ViewController.m not.. so.. what I didn't understand there..?
vogueestylee is offline   Reply With Quote
Old 02-02-2012, 03:30 PM   #2 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

The problem is you are allocating and adding data to your array after you present the viewController.

Move the red lines in your App Delegate to the top of the didFinishLaunchingWithOption function and it should work.
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z
apatsufas is offline   Reply With Quote
Old 02-02-2012, 03:36 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by apatsufas View Post
The problem is you are allocating and adding data to your array after you present the viewController.

Move the red lines in your App Delegate to the top of the didFinishLaunchingWithOption function and it should work.
Although putting globals in your app delegate is not recommended. It's cleaner and better to use a data container singleton. Take a look at the password generator app tutorial linked in my signature. It shows how to create a data container singleton.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-03-2012, 03:01 AM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

apatsufas - thanks!

duncan - thank you I will read this. and back to my question about "basics" of programming..? it is better to do thinks like downloading and so on in appdelegate or it is also correct in viewController.m file? how you, professionals do this?
vogueestylee is offline   Reply With Quote
Old 02-03-2012, 03:11 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 69
AndreaT is on a distinguished road
Default

Totally agree with Duncan C. Usually if we want to share an Object trough different view is better and cleaner to have a singleton class managing this.

In a standard app is better if you create more then one singleton to manage different thing. For exmple, audioManager, dataManager and so on and recall this as you need.

Here you can find a code snippet to create a singleton class [Objective C] ARC Compatible Singleton - Pastebin.com

My2cent
AndreaT is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 397
15 members and 382 guests
Atatator, chiataytuday, dre, FrankWeller, imac74, ipodphone, jeroenkeij, kukat, LunarMoon, MAMN84, n00b, QuantumDoja, Retouchable, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,675
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Retouchable
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:06 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0