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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 09-02-2010, 01:44 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 67
Default How to call a method and set a variable from subview?

hi all,
i have a viewcontroller, im adding a subviewcontroller to it. this subviewcontroller use a subview (subclassed UIView). there is a button in subview.
if the user press the button, i want to set a varibale in Mainviewcontroller, then call RefreshPage method.

layout:
MainViewController;
NSINteger xyzID;
-(void) RefreshPage;

i add subViewController in viewdidload method.

Code:
	subVC=[[SubViewController alloc] initWithType:1];
	[self.view addSubview:subVC.view];
i am adding subview controller 3 different viewcontrollers (like mainviewcontroller).
how can i set xyzID then call RefreshPage method ?
i think i should use protocol or delegate.
thanks in advance.
tester is offline   Reply With Quote
Old 09-02-2010, 02:28 PM   #2 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 362
Default

You should read the View Controller Programming Guide

View Controller Programming Guide for iOS: About View Controllers

In particular

"Each custom view controller object you create is responsible for managing exactly one screen’s worth of content. The one-to-one correspondence between a view controller and a screen is a very important consideration in the design of your application. You should not use multiple custom view controllers to manage different portions of the same screen. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content."

If for some reason you wanted to do that - you could create a protocol for a delegate to implement that the subview controller would invoke the delegate when things of interest happen.
timle8n1 is offline   Reply With Quote
Old 09-02-2010, 02:39 PM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 67
Default

Quote:
Originally Posted by timle8n1 View Post
You should read the View Controller Programming Guide

View Controller Programming Guide for iOS: About View Controllers

In particular

"Each custom view controller object you create is responsible for managing exactly one screen’s worth of content. The one-to-one correspondence between a view controller and a screen is a very important consideration in the design of your application. You should not use multiple custom view controllers to manage different portions of the same screen. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content."

If for some reason you wanted to do that - you could create a protocol for a delegate to implement that the subview controller would invoke the delegate when things of interest happen.
yes i know it, thats why i said i should use protocol or delegate.
what do you advise if you use same button series (in a scrollview)on the top of your 3 viewcontrollers? buttons tag and title's are different for viewcontrollers. thats why i separated buttons into a subview and wrote sub viewcontroller.
i dont how to use protocols.
tester is offline   Reply With Quote
Old 09-02-2010, 04:57 PM   #4 (permalink)
Use [code] tags please
 
Join Date: Jun 2009
Location: Jacksonville, FL
Posts: 362
Default

Quote:
Originally Posted by tester View Post
yes i know it, thats why i said i should use protocol or delegate.
what do you advise if you use same button series (in a scrollview)on the top of your 3 viewcontrollers? buttons tag and title's are different for viewcontrollers. thats why i separated buttons into a subview and wrote sub viewcontroller.
i dont how to use protocols.
IMHO, you are close but instead of a sub viewcontroller - you should just subclass UIView. Call it ScrollButtonView or something. Design a protocol for delegates to implement to be informed of button presses. Register your View Controller as the delegate. This is how UITableView works.

Here is an partial example
ScrollButtonView.h
Code:
#import <UIKit/UIKit.h>

@protocol ScrollButtonViewDelegate;

@interface ScrollButtonView : UIView {
	id delegate;
	UIButton *button1;
	UIButton *button2;
	UIButton *button3;
}

@property (assign) id<ScrollButtonViewDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;

- (IBAction)internalButtonPressed:(id)sender;

@end 

@protocol ScrollButtonViewDelegate

- (void)buttonPressed:(id)sender;

@end
ScrollButtonView.m
Code:
#import "ScrollButtonView.h"

@implementation ScrollButtonView

@synthesize delegate;
@synthesize button1;
@synthesize button2;
@synthesize button3;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)internalButtonPressed:(id)sender {
	if (delegate && [delegate respondsToSelector:@selector(buttonPressed:)]) {
		[delegate buttonPressed:sender];
	}
}


- (void)dealloc {
    [super dealloc];
}

@end
timle8n1 is offline   Reply With Quote
Old 09-02-2010, 10:31 PM   #5 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 67
Default

Quote:
Originally Posted by timle8n1 View Post
IMHO, you are close but instead of a sub viewcontroller - you should just subclass UIView. Call it ScrollButtonView or something. Design a protocol for delegates to implement to be informed of button presses. Register your View Controller as the delegate. This is how UITableView works.

Here is an partial example
ScrollButtonView.h
Code:
#import <UIKit/UIKit.h>

@protocol ScrollButtonViewDelegate;

@interface ScrollButtonView : UIView {
	id delegate;
	UIButton *button1;
	UIButton *button2;
	UIButton *button3;
}

@property (assign) id<ScrollButtonViewDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;

- (IBAction)internalButtonPressed:(id)sender;

@end 

@protocol ScrollButtonViewDelegate

- (void)buttonPressed:(id)sender;

@end
ScrollButtonView.m
Code:
#import "ScrollButtonView.h"

@implementation ScrollButtonView

@synthesize delegate;
@synthesize button1;
@synthesize button2;
@synthesize button3;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)internalButtonPressed:(id)sender {
	if (delegate && [delegate respondsToSelector:@selector(buttonPressed:)]) {
		[delegate buttonPressed:sender];
	}
}


- (void)dealloc {
    [super dealloc];
}

@end
everything is ok, but there is no code for "Register your View Controller as the delegate". how to register my ViewController for delegate?
i couldnt register it. i dont know where to.
thats why it doesnt respond the selector. i will use this ScrollButtonView in 3 viewcontrollers. how to register delegate for them?

thanks...
tester is offline   Reply With Quote
Old 09-02-2010, 10:42 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 67
Default

i tried some codes, i hope its right.

in ViewControllers.h file:
#import "ScrollButtonView.h"
@interface MainViewController : UIViewController < ScrollButtonViewDelegate >
// setting property ...
in ViewControllers.m file
// setting @synthesize ..
// also dealloc

in viewdidload:

Code:
subView=[[ScrollButtonView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[subView setDelegate:self];
[self.view addSubview:subView];
it works, thanks...

Last edited by tester; 09-02-2010 at 11:34 PM.
tester 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: 269
17 members and 252 guests
ADY, Alsahir, dacapo, Dani77, Desert Diva, Duncan C, F_Bryant, Grinarn, HemiMG, jansan, linkmx, M@realobjects, macquitzon216, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,761
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

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