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 10-26-2009, 04:03 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 48
encryption is on a distinguished road
Question Calling methods in parent's object Works with warnings

Hello,

I am writing an application for the Apple iPhone / Touch. Everything is going pretty well though one thing I am doing is generating warnings. Not errors but warnings, the applications still builds, installs, and runs. Though these warnings are making me think there must be a more proper way to code the functionality that I am developing.

This is an example of what I am doing and the warnings it generates:

I have a subclassed UIViewController that adds several subviews. In some of these subviews I need the ability to have the subview call methods that are located in the UIViewController object that loaded it.

This is how I am currently doing it:

In the UIViewController's .h file I have something like this:
Code:
#import <UIKit/UIKit.h>
#import "TimedBarView.h"

@interface SomeViewController : UIViewController
{

	IBOutlet TimedBarView *timedBarSubView;
	
}

@property (nonatomic,retain) IBOutlet TimedBarView *timedBarSubView;

- (void)timedBarDone;
In the subclassed UIViewController's .m file I have something like this:
Code:
#import "SomeViewController.h"

@implementation SomeViewController

@synthesize timedBarSubView;
In this same file I have some code like this in the - (void)viewDidLoad method:
Code:
self.timedBarSubView.parent = self;
This code sets a UIViewController pointer called parent that is defined in TimedBarView.h as:
Code:
@interface TimedBarView : UIViewController
{
	UIViewController *parent;
}

@property (nonatomic, retain)	UIViewController		*parent;

- (void)updateTimerBar;
In this same file I have code in the updateTimerBar method that does this:
Code:
if (progress >= 0)
{
	[timer invalidate];		
	[self.view removeFromSuperview];

	[parent timedBarDone];
}
The line [parent timedBarDone]; in the above code is what generates the following warning:

'UIViewController' may not respond to '-timedBarDone' (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)

Though, there is a matching method and it does get called and functions as expected. Does anyone have any idea of what is causing this warning and how I can have a subview call its parents methods without generating such a warning?

One example of why it's so critical that the child be able to call methods in the parent object is that this timedBarView object has a button on it that enables the user to have this subview slide off screen (except for a small button that allows it to return into full view). A subview, as far as I can tell, is oblivious as to its geometry within its parent's UIView. Secondly, it is my hope that I can write such a class so that I can create several of these objects to be used throughout my application and possibly make it part of a static library that I could link against to build other applications with. So, with this in mind the object will not know who it's parent is until the code is executed.

I know the parent pointer is not included in the the default variables like self is. I thought the way I approached this made sense and it does work, but this warning has me concerned.

Thanks in advance for reading this post and any advice you can offer.

Nick Powers

Last edited by encryption; 10-26-2009 at 04:06 AM. Reason: Left out part of the example code
encryption is offline   Reply With Quote
Old 10-26-2009, 04:47 AM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default

I think you need to learn how to use @protocol in Obj C
__________________
lazy blogs
The Lord's holy name be praised.
david_david is offline   Reply With Quote
Old 10-26-2009, 06:21 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 48
encryption is on a distinguished road
Arrow @protocol huh?

Quote:
Originally Posted by david_david View Post
I think you need to learn how to use @protocol in Obj C
OK...

If it is @protocol functionality that I need to learn then does anyone know of a good online source for learning @protocol? I've looked at it on Apple's site and the way it is described there just confuses me more each time I read about it.

Also, what is it that I am trying to do that would require @protocol? I've looked at it a bit and it seems to be quite bit more than what I need. It seems funny that such overhead would be required to do such a simple task.

Is there any downside, other than an annoyance to me when building, if I just ignore the warnings and keep doing it the way I have described?

If anyone is reading this and is fluent in using @protocol then could you please give me an example on how I would use it to allow my subview the ability to call a single method from its parent's object?

Thanks again!

Nick Powers
encryption is offline   Reply With Quote
Old 10-26-2009, 06:42 AM   #4 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default

protocol in objective C is similar to java interfaces

haven't check/execute the following code.. just a skeleton


//self.timedBarSubView.parent = self;
self.timedBarSubView.delegate = self;


in TimedBarView.h

Code:
@protocol MyTestDelegate;


@interface TimedBarView : UIViewController
{
	id <MyTestDelegate> delegate;
}

@property (nonatomic, assign) id <MyTestDelegate> delegate;

@end

@protocol MyTestDelegate <NSObject>

- (void)timedBarDone;

@end

in TimedBarView.m

Code:
@synthesize delegate;


if (progress >= 0)
{
	[timer invalidate];		
	[self.view removeFromSuperview];

	[self.delegate timedBarDone];
}

-(void)dealloc{

delegate = nil;
...
}
__________________
lazy blogs
The Lord's holy name be praised.

Last edited by david_david; 10-26-2009 at 07:09 AM.
david_david is offline   Reply With Quote
Old 10-26-2009, 06:52 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 17
Mehuge is on a distinguished road
Default

You could fix your warning as follows:

Either make parent a SomeViewController *

Code:
SomeViewController *parent;
or in updateTimerBar as long as you know the type of parent at that point, you can cast it to the appropriate type, e.g. SomeViewController * or whatever it's type is at that time.

Code:
[(SomeViewController *)parent timedBarDone];
HOWEVER, using @protocol and a delegate would be the normal way to achieve what your doing.

Code:
@protocol TimedBarDelegate
- (void) timedBarDone:(id)sender;
@end

@interface TimedBarView : UIViewController {
      id <TimedBarDelegate> delegate;
}
@end
in SomeViewControler make it implement the TimedBarDelegate protocol

Code:
@implementation SomeViewController : UIViewController <TimedBarDelegate>
then during init after a TimedBarView is created, do

Code:
timedBarView.delegate = self;
and in dealloc (important)

Code:
timedBarView.delegate = null;
[timedBarView release];
and also add the delegate method

Code:
- (void)timedBarDone:(id)sender {
     // do whatever you need to do
}
then back in TimedBarView when you want to tell the parent (or now its delegate) something

Code:
if (progress >= 0)
{
	[timer invalidate];		
	[self.view removeFromSuperview];
	if (delegate) [delegate timedBarDone:self];
}

Last edited by Mehuge; 10-26-2009 at 06:54 AM.
Mehuge is offline   Reply With Quote
Old 10-26-2009, 06:56 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 48
encryption is on a distinguished road
Thumbs up Woot!

Much thanks David!

That's the clearest example I have seen to date. You should write a tutorial, if your into that kind of thing.

Anyways, just wanted to show my appreciation. Now, I'm off to give it a try. Thank goodness for snapshots in XCode!

Nick Powers

Quote:
Originally Posted by david_david View Post
haven't check/execute the following code.. just a skeleton


//self.timedBarSubView.parent = self;
self.timedBarSubView.delegate = self;


in TimedBarView.h

Code:
@protocol MyTestDelegate;


@interface TimedBarView : UIViewController
{
	id <MyTestDelegate> delegate;
}

@property (nonatomic, assign) id <MyTestDelegate> delegate;

@end

@protocol MyTestDelegate <NSObject>

- (void)timedBarDone;

@end

in TimedBarView.m

Code:
@synthesize delegate;


if (progress >= 0)
{
	[timer invalidate];		
	[self.view removeFromSuperview];

	[self.delegate timedBarDone];
}

-(void)dealloc{

delegate = nil;
...
}
encryption is offline   Reply With Quote
Old 10-26-2009, 07:01 AM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 370
david_david is on a distinguished road
Default

oh.. I just forgot to say about implementing this protocol in needed class otherwise this "self.timedBarSubView.delegate = self;" won't work (thanks to Mehuge to point this...)

@implementation SomeViewController : UIViewController <MyTestDelegate>
__________________
lazy blogs
The Lord's holy name be praised.

Last edited by david_david; 10-26-2009 at 07:04 AM.
david_david is offline   Reply With Quote
Old 10-26-2009, 07:59 AM   #8 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 48
encryption is on a distinguished road
Cool Works flawlessly with just 1 minor adjustment

Quote:
Originally Posted by david_david View Post
oh.. I just forgot to say about implementing this protocol in needed class otherwise this "self.timedBarSubView.delegate = self;" won't work (thanks to Mehuge to point this...)

@implementation SomeViewController : UIViewController <MyTestDelegate>
David,

Thanks to you and Mehuge I'm golden now. Works 100% with no warnings!

The only thing I had to change, just so others might learn from this wonderful thread, was to make SomeViewController implement the TimedBarDelegate protocol I needed this line of code:

@interface SomeViewController : UIViewController <TimedBarDelegate>

in SomeViewController.h

and kept:

@implementation SomeViewController

in SomeViewController.m

Thanks again,

Nick Powers
encryption is offline   Reply With Quote
Reply

Bookmarks

Tags
callback, delegate, parent, subview, uiview

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: 344
10 members and 334 guests
alexP, ClerurcifeDer, givensur, glenn_sayers, guusleijsten, ipodphone, JmayLive, Punkjumper, whitey99, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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