Amazon.com Widgets General method callable from different UIViewControllers?
Welcome, Guest. Please login or register.
Did you miss your activation email?
October 30, 2014, 06:41:17 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Chapter Study
| | |-+ Chapter 21 - Writing iPhone Applications
| | | |-+ General method callable from different UIViewControllers?
Pages: [1] Go Down
Print
Author Topic: General method callable from different UIViewControllers? (Read 1263 times)
dcc1165
Newbie
*
Posts: 14


Email




on: March 02, 2010, 05:27:24 PM

I just finished my first iPhone app and there's one thing that's annoying me...I have a single UITableView that's used to edit 1 of 4 arrays.  The first 3 allow the user to add or delete items, the 4th only allows deleting (it's a "favorites" list that is made up of info derived from the first 3 arrays).

When the app first starts, if it's a first-time run, I save the default data to a plist (a dictionary of arrays)  in the app sandbox (via a saveData method in the main UIView  controller that comes up when the app starts).

After changes are made via the list editor (UITableView controller), I want the user data to be saved again by calling "saveData" method again.  The problem is, I had to copy the saveData method code into the tableEditViewController.m file as well as the main UIView controller.

My question:  Is there a way to put the saveData (or any other common method) in a single .m file so it can be called from anywhere without having to copy the same method over and over?

Thanks in advance Smiley
-= Don =-
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: March 02, 2010, 09:37:54 PM

You can put the saveData method in your appDelegate class.   Then you can call that method from your view controllers by importing the appDelegate header file and inserting the following code whenever you want to use the method.  

(Assume your appDelegate class is called Prog1AppDelegate)

Code: (Objective-C)
#import "Prog1AppDelegate.h"
...
     // following appears in some method in one your view controller classes
     // save the data
    Prog1AppDelegate *appdelegate =  [UIApplication sharedApplication].delegate;
    [appDelegate  saveData];

...

If you need to transfer data, you can extend the method to take arguments and pass the data to the method that way.

Cheers,

Steve Kochan
Last Edit: March 02, 2010, 09:42:30 PM by skochan Logged
dcc1165
Newbie
*
Posts: 14


Email




Reply #2 on: March 03, 2010, 12:13:08 AM

Hmmm...I hadn't thought of doing that.  All of the examples I've seen don't ever touch the appDelegate code (other than to add the main view to the window), so I wasn't sure if that was politically correct.

A little more detail on where the data comes from...I read the default (or user-saved) .plist when the first view controller loads.  The first view controller acts as a navigation controller and is able to shuffle the arrays around to the other views without a problem.

Now, if I put the saveData method in the app delegate as you described,  it would make things a lot easier; however, I'm thinking I will have to pass the array(s) to the saveData method (obviously).  I read something in the Objective-C docs on Apple's website that talked about calling a method like this:

Code: (Objective-C)
[receiver makeGroup:group, memberOne, memberTwo, memberThree];

I'm thinking I can pass my arrays to a method like this, but how do you define this particular method (note the commas between the "group" and "memberXXXX" parameters)?.  Apple docs tend to have a few holes in them -- specifically telling you how to define something that's used in one of their examples -- which is why I bought your book in the first place... Smiley

Thanks Smiley
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: March 03, 2010, 01:52:24 AM

Saving the application's data is consistent with the type of work the app delegate class does.  Instead of getting involved with a variable argument list (which is what passing the arguments separated by commas implies), just write a method that takes three arguments.  If you need it to handle fewer than three in some cases (that wasn't clear from your example), then just pass in nil as appropriate.

Cheers,

Steve Kochan
Last Edit: March 03, 2010, 01:53:56 AM by skochan Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.