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 03-09-2010, 12:19 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default calling a parent view's method from a subview

See 4th post

I'm working with a multiple view application. I have a view switching class that handles all the removing and adding of content views to the main window. This work perfectly when linking buttons in the main window view (whose class is the switching class) to the functions in the switching class. However, sometimes I need to call these functions (functions add/remove subviews so they can be displayed) from within a content view (subview). I've tried a lot of different things, but I can't seem to get anything to work.

Creating pointer to the swtich class doesnt work:

//content view class m file
doSomething{
viewSwitcher *v = [[viewSwitcher alloc] init];
[v switchView];

swtichView being the function I want to call. It is (and must be) an instance method ( - ).

I've read about delegates, but can't find any tutorials that are helpful.

Any ideas?

Last edited by Journer; 03-09-2010 at 02:01 PM.
Journer is offline   Reply With Quote
Old 03-09-2010, 01:01 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 1,018
Tambourin is on a distinguished road
Default

MyMainView *myMainView = (MyMainView *)self.superview;
[myMainView switchView];
Tambourin is offline   Reply With Quote
Old 03-09-2010, 01:46 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default

Quote:
Originally Posted by Tambourin View Post
MyMainView *myMainView = (MyMainView *)self.superview;
[myMainView switchView];

tried this. got this error:

MainMenuViewController.m:18: error: request for member 'superview' in something not a structure or union
Journer is offline   Reply With Quote
Old 03-09-2010, 01:54 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default

//SwitchViewController.h

Code:
#import <UIKit/UIKit.h>
@class MainMenuViewController;
@class GlassRepairsViewController;

@interface SwitchViewController : UIViewController {
	MainMenuViewController *mainMenuViewController;
	GlassRepairsViewController *glassRepairsViewController;

}


@property (retain,nonatomic) MainMenuViewController *mainMenuViewController;
@property (retain,nonatomic) GlassRepairsViewController *glassRepairsViewController;

-(IBAction)goToGlassRepairs;
-(IBAction)goToMainMenu;

@end
//switch view controller m file
Code:
#import "SwitchViewController.h"
#import "MainMenuViewController.h"
#import "GlassRepairsViewController.h"


@implementation SwitchViewController
@synthesize MainMenuViewController;
@synthesize GlassRepairsViewController;

- (void)viewDidLoad {
	MainMenuViewController *mainMenuController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];
	self.mainMenuViewController = mainMenuController;
	[self.view insertSubview:mainMenuController.view atIndex:0];
	[mainMenuController release];
    [super viewDidLoad];
}
//These IBActions are linked to buttons on the view that is linked to this class (swtichViewController)  This works perfectly for showing different nibs

-(IBAction) goToGlassRepairs
{
	if(self.glassRepairsViewController.view.superview == nil)
	{
		if(self.glassRepairsViewController == nil)		
		{
			GlassRepairsViewController *glassRepairsController = [[GlassRepairsViewController alloc] initWithNibName:@"GlassRepairsView" bundle:nil];
			self.GlassRepairsViewController =glassRepairsController;
			[glassRepairsController release];		
		}
		[mainMenuViewController.view removeFromSuperview];
		[self.view insertSubview:glassRepairsViewController.view atIndex:0];	
	}	
}

-(IBAction) goToMainMenu
{
	if(self.mainMenuViewController.view.superview == nil)
	{
		if(self.mainMenuViewController == nil)		
		{
			 MainMenuViewController *mainMenuController = [[ MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];
			self.mainMenuViewController = mainMenuController;
			[mainMenuController release];		
		}
		[glassRepairsViewController.view removeFromSuperview];
		[self.view insertSubview:mainMenuViewController.view atIndex:0];	
	}	
	
}
//MainMenuViewController.h
Code:
#import <UIKit/UIKit.h>

@class SwitchViewController;


@interface MainMenuViewController : UIViewController {

}

-(IBAction)goToGlass;


@end
//MainMenuViewController.m

Code:
#import "MainMenuViewController.h"
#import "SwitchViewController.h"


@implementation MainMenuViewController


-(IBAction)goToGlass
{
  //This is where I want to call the goToGlassRepairs method that is inside SwitchViewController's class...

	SwitchViewController *switch= (SwitchViewController *)self.superview;
	[switch goToGlassRepairs];	
	
}
SwitchViewController is the class that handles which nibs to display and remove.
MainMenu and glassRepairs are content views. However, MainMenu needs to access the methods in the switchviewcontroller class to allow navigation from the content view.

Last edited by Journer; 03-09-2010 at 01:58 PM.
Journer is offline   Reply With Quote
Old 03-09-2010, 03:40 PM   #5 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 6
coconnor is on a distinguished road
Default

If you are going back to the parent view controller why don't you just pop or dismiss the second controller as in

[self.navigationController popViewControllerAnimated:YES];

or

[self dismissModalViewControllerAnimated:YES];

Good luck
Cheryl
coconnor is offline   Reply With Quote
Old 03-09-2010, 03:56 PM   #6 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default

I tried this:

Code:
SwitchViewController *s = (SwitchViewController *)[[UIApplication sharedApplication] delegate];
	
[s goToGlassRepairs];
and it compiles with no warnings. however, when you click the button linked to this method, it crashes with the following errors:

[29535:207] *** -[GoAppDelegate goToGlassRepairs]: unrecognized selector sent to instance 0x3b26620

[29535:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***

-[GoAppDelegate goToGlassRepairs]: unrecognized selector sent to instance 0x3b26620'

.....

Last edited by Journer; 05-06-2010 at 09:55 AM.
Journer is offline   Reply With Quote
Old 03-09-2010, 04:05 PM   #7 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default

Quote:
Originally Posted by coconnor View Post
If you are going back to the parent view controller why don't you just pop or dismiss the second controller as in

[self.navigationController popViewControllerAnimated:YES];

or

[self dismissModalViewControllerAnimated:YES];

Good luck
Cheryl
not really sure what you are talking about, but I tried the above code. It compiles, but nothing happens when I click the button linked to the method I put this in.
Journer is offline   Reply With Quote
Old 03-09-2010, 05:26 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,896
harrytheshark is on a distinguished road
Default

That's because your SwitchViewController isn't your app delegate.
harrytheshark is offline   Reply With Quote
Old 03-09-2010, 09:08 PM   #9 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default

ok, even when I try:

switchViewController *s = [[SwitchViewController alloc] init];
[s goToGlassRepair];

it gives me warnings saying s may not respond to goToGlassRepair
Journer is offline   Reply With Quote
Old 03-10-2010, 11:12 AM   #10 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 49
Journer is on a distinguished road
Default

finally got this to work:

what I did was added a function in the app delegate that called the display function in the switchview controller. then i imported the app delegate into my main menu and made a function that called the app delegate's function.
Journer 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: 344
9 members and 335 guests
akacaj, c2matrix, esoteric, givensur, HemiMG, mjnafjke, Pudding, SLIC, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

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