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 Tools & Utilities

Reply
 
LinkBack Thread Tools Display Modes
Old 12-26-2009, 01:04 AM   #1 (permalink)
Game Dev
 
mikolo's Avatar
 
Join Date: Dec 2009
Location: Munich
Posts: 31
mikolo is on a distinguished road
Default call a function from UIView in UIViewController?

Hi,

i have defined two UIViews and one action in my UIViewController like this:

PHP Code:
#import <UIKit/UIKit.h>
#import "LoginView.h"
#import "ContactListView.h"

@interface NowHereViewController UIViewController {
    
IBOutlet LoginView *loginView;
    
IBOutlet ContactListView *contactListView;
        
}

-(
void)swichView:(id)sender;

@
end 

first i load the loginView in my view in NowHereViewController.m like this:

PHP Code:
- (void)viewDidLoad {
    
NSLog(@" ---- viewDidLoad ---- ");
    
    [
self.view addSubview:loginView];
    [
loginView initLoginView];
        [
super viewDidLoad];
    

now i want to call the function swichView in the NowHereViewController from the loginView again?

How can i do this?
Thanxxx for help.
mikolo is offline   Reply With Quote
Old 12-26-2009, 02:41 PM   #2 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
kierster is on a distinguished road
Default

Quote:
Originally Posted by mikolo View Post
Hi,

i have defined two UIViews and one action in my UIViewController like this:

PHP Code:
#import <UIKit/UIKit.h>
#import "LoginView.h"
#import "ContactListView.h"

@interface NowHereViewController UIViewController {
    
IBOutlet LoginView *loginView;
    
IBOutlet ContactListView *contactListView;
        
}

-(
void)swichView:(id)sender;

@
end 

first i load the loginView in my view in NowHereViewController.m like this:

PHP Code:
- (void)viewDidLoad {
    
NSLog(@" ---- viewDidLoad ---- ");
    
    [
self.view addSubview:loginView];
    [
loginView initLoginView];
        [
super viewDidLoad];
    

now i want to call the function swichView in the NowHereViewController from the loginView again?

How can i do this?
Thanxxx for help.
I had this same problem myself.
There's probably an easier way but here's how I solved it.

Add this to your view.h file
Code:
NowHereViewController *controller;
Make it a property in the .h and synthesize it in the .m file.
You will use controller to access the controller.

In the viewDidLoad of your viewcontroller add this line:
Code:
self.view.controller = self;
This sends a pointer of your view controller to the view, so it can access it.

Just be careful with this though! If the viewcontroller is non-existent the view will crash when its calls for it. You need to ensure that if a new view is made it will also know of this change. I add a if(self.controller != nil) whenever I call the controller, just so that if I forget to set the controller, it won't ruin everything!

Hope it works for you!
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps
kierster is offline   Reply With Quote
Old 12-27-2009, 09:29 AM   #3 (permalink)
Game Dev
 
mikolo's Avatar
 
Join Date: Dec 2009
Location: Munich
Posts: 31
mikolo is on a distinguished road
Default

Quote:
Originally Posted by kierster View Post
I had this same problem myself.
There's probably an easier way but here's how I solved it.

Add this to your view.h file
Code:
NowHereViewController *controller;
Make it a property in the .h and synthesize it in the .m file.
You will use controller to access the controller.

In the viewDidLoad of your viewcontroller add this line:
Code:
self.view.controller = self;
This sends a pointer of your view controller to the view, so it can access it.

Just be careful with this though! If the viewcontroller is non-existent the view will crash when its calls for it. You need to ensure that if a new view is made it will also know of this change. I add a if(self.controller != nil) whenever I call the controller, just so that if I forget to set the controller, it won't ruin everything!

Hope it works for you!

Hi, thank you but i become the error: expected specifier-qalifier-list before 'NowHereViewController'!

I don`t konw what does this means? Can you help me again? I read also about protocol... but i don`t understand ... the apple help ist realy difficult!
mikolo is offline   Reply With Quote
Old 12-27-2009, 10:30 AM   #4 (permalink)
Game Dev
 
mikolo's Avatar
 
Join Date: Dec 2009
Location: Munich
Posts: 31
mikolo is on a distinguished road
Default

Quote:
Originally Posted by mikolo View Post
Hi, thank you but i become the error: expected specifier-qalifier-list before 'NowHereViewController'!

I don`t konw what does this means? Can you help me again? I read also about protocol... but i don`t understand ... the apple help ist realy difficult!

OK i got it now.. but a little bit different and with one warning! Here ist my result:

In NowHereViewController.m i wrote:

PHP Code:
- (void)viewDidLoad {
    
NSLog(@" ---- viewDidLoad ---- ");
    
    [
self.view addSubview:loginView];
    
    
loginView.controller self;
    [
loginView initLoginView];
    
    [
super viewDidLoad];
    
    

In LoginView.h i have:

PHP Code:
@interface LoginView UIView {

    
UIViewController *controller;
}

@
property (nonatomicretainUIViewController *controller
in Login.m i can call the method like this:

PHP Code:
[controller swichView:(@"ContactListView")]; 
but here comes a warning like this: 'UIViewController' may not respond to '-switchView:'

Maybe someone knows.. how i fix also this warning.. but when i try to do all with NowHereViewController it doesn`t work!
mikolo is offline   Reply With Quote
Old 12-27-2009, 10:45 AM   #5 (permalink)
Registered Member
 
kierster's Avatar
 
Join Date: Mar 2009
Location: Canada!
Posts: 261
kierster is on a distinguished road
Default

Quote:
Originally Posted by mikolo View Post
OK i got it now.. but a little bit different and with one warning! Here ist my result:

In NowHereViewController.m i wrote:

PHP Code:
- (void)viewDidLoad {
    
NSLog(@" ---- viewDidLoad ---- ");
    
    [
self.view addSubview:loginView];
    
    
loginView.controller self;
    [
loginView initLoginView];
    
    [
super viewDidLoad];
    
    

In LoginView.h i have:

PHP Code:
@interface LoginView UIView {

    
UIViewController *controller;
}

@
property (nonatomicretainUIViewController *controller
in Login.m i can call the method like this:

PHP Code:
[controller swichView:(@"ContactListView")]; 
but here comes a warning like this: 'UIViewController' may not respond to '-switchView:'

Maybe someone knows.. how i fix also this warning.. but when i try to do all with NowHereViewController it doesn`t work!
Don't worry about the warning too much:
Switch UIViewController with NowHereViewController. And add this line above it.
Code:
@class NowHereViewController;
@interface LoginView : UIView {

	NowHereViewController *controller;
}
Be sure you add "NowHereViewController.h" to your .m file.
Code:
#import "NowHereViewController.h"
Now you should be set!
__________________
Check out some of my apps:
Boltz ($0.99)
FreeBoltz (FREE)
Cross Digits ($2.99) [Universal!]
Cross Digits Lite (FREE) [Universal!]
Targets ($0.99) (Facebook | YouTube Demo)
Greg's Apps
kierster is offline   Reply With Quote
Old 12-27-2009, 12:16 PM   #6 (permalink)
Game Dev
 
mikolo's Avatar
 
Join Date: Dec 2009
Location: Munich
Posts: 31
mikolo is on a distinguished road
Default

Quote:
Originally Posted by kierster View Post
Don't worry about the warning too much:
Switch UIViewController with NowHereViewController. And add this line above it.
Code:
@class NowHereViewController;
@interface LoginView : UIView {

	NowHereViewController *controller;
}
Be sure you add "NowHereViewController.h" to your .m file.
Code:
#import "NowHereViewController.h"
Now you should be set!
Yeppa thanx a lot it works! I love you
mikolo is offline   Reply With Quote
Old 03-17-2010, 10:15 AM   #7 (permalink)
Registered Member
 
Join Date: Mar 2010
Posts: 3
pandacat is on a distinguished road
Thumbs up Thanks Kierster!!!

Quote:
Originally Posted by mikolo View Post
Yeppa thanx a lot it works! I love you
Reading this post solved the same question I was having, so now I don't have to bug somebody else. 16 and so smart - way to go!! Thanks a bunch
pandacat is offline   Reply With Quote
Old 08-24-2010, 07:52 PM   #8 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 1
xoshi101 is on a distinguished road
Default

Quote:
Originally Posted by kierster View Post
In the viewDidLoad of your viewcontroller add this line:
Code:
self.view.controller = self;
This sends a pointer of your view controller to the view, so it can access it.
When I put this in my MainViewController.m file, it causes a build error.

Here's what I want to do. There's a void in my MainViewController.m file that I want to be able to call from MainView.m. This void needs to display the in-app email form.

MainViewController.h
Code:
- (void)showEmailModalView:(NSString*) storyText;
This is the function I want to call. storyText is the string that I want to display in the body of the email.

MainView.h
Code:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@class MainViewController;
@interface MainView : UIView {
	IBOutlet UITextField *userName;
	IBOutlet UISegmentedControl *userGender;
	IBOutlet UITextView *storyBox;
	
	MainViewController *controller;
}

@property (nonatomic, retain) MainViewController *controller;
MainView.m
Code:
#import "MainView.h"
#import "MainViewController.h"
//import email sheet stuff
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@implementation MainView

@synthesize controller;

//bunch of other program code

//email function
-(IBAction)emailStory {
	//check if user entered a name
	NSLog(@"MainView.m -(IBAction)emailStory called");
	if(storyName==nil) {
		//display warning
	} else {
		NSString *storyTextToEmail = [storyBox text];
		[controller showEmailModalView:storyTextToEmail];
	}
}
XCode isn't giving me warnings or errors, but when I put self.view.controller = self; or MainView.controller = self; in the viewDidLoad as stated above, it gives me errors.

If I use self.view.controller = self;, XCode says
error: request for member 'controller' in something not a structure or union

If I use MainView.controller = self;, XCode says
error: accessing unknown 'setController:' class method
error: object cannot be set - either readonly property or no setter found

Any suggestions? I'm completely stumped by this.
xoshi101 is offline   Reply With Quote
Old 09-27-2010, 01:15 AM   #9 (permalink)
Registered Member
 
Join Date: Sep 2010
Location: India
Posts: 26
kgupta is on a distinguished road
Default

I am getting same errors, please guide.

Thanks.

Quote:
Originally Posted by xoshi101 View Post
When I put this in my MainViewController.m file, it causes a build error.

Here's what I want to do. There's a void in my MainViewController.m file that I want to be able to call from MainView.m. This void needs to display the in-app email form.

MainViewController.h
Code:
- (void)showEmailModalView:(NSString*) storyText;
This is the function I want to call. storyText is the string that I want to display in the body of the email.

MainView.h
Code:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@class MainViewController;
@interface MainView : UIView {
	IBOutlet UITextField *userName;
	IBOutlet UISegmentedControl *userGender;
	IBOutlet UITextView *storyBox;
	
	MainViewController *controller;
}

@property (nonatomic, retain) MainViewController *controller;
MainView.m
Code:
#import "MainView.h"
#import "MainViewController.h"
//import email sheet stuff
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>

@implementation MainView

@synthesize controller;

//bunch of other program code

//email function
-(IBAction)emailStory {
	//check if user entered a name
	NSLog(@"MainView.m -(IBAction)emailStory called");
	if(storyName==nil) {
		//display warning
	} else {
		NSString *storyTextToEmail = [storyBox text];
		[controller showEmailModalView:storyTextToEmail];
	}
}
XCode isn't giving me warnings or errors, but when I put self.view.controller = self; or MainView.controller = self; in the viewDidLoad as stated above, it gives me errors.

If I use self.view.controller = self;, XCode says
error: request for member 'controller' in something not a structure or union

If I use MainView.controller = self;, XCode says
error: accessing unknown 'setController:' class method
error: object cannot be set - either readonly property or no setter found

Any suggestions? I'm completely stumped by this.
kgupta 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: 379
10 members and 369 guests
apatsufas, comicool, husthlj, illogical, LegionMD, LunarMoon, mer10, Murphy, padsoftware, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

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