Register    Login    Forum    Search    FAQ

Board index » Programming » Objective-C / iPhone Development




 Page 1 of 1 [ 5 posts ] 



Author Message
 Post subject: Newbie!! Please Help me!!!
 Post Posted: Fri Jul 30, 2010 7:23 pm 
Offline
n00b
n00b

Joined: Fri Jul 30, 2010 3:36 pm
Posts: 4
Hi everyone! ... :D

First of all!: i'm a fu$%ing newbie! :oops:

I'm trying to use a Function called "PrintingText" declared on a personal class called MyMethods, inside my ViewController Method. Simple: Click a button and say "This is the text" in my TextField. But when I clicked the button, my TextField doesn't show anything! ... Please, help me:

This is the code:

MyMethods.h //

#import <Foundation/Foundation.h>
#import "testViewController.h"
@interface MyMethods : testViewController {
}
-(void) PrintingText: (id)source;
@end

MyMethods.m //

#import "MyMethods.h"
@implementation MyMethods
-(void) PrintingText: (id)source{
NSString * finalText = (NSString *) source;
[sourceText setText:finalText];
}
@end

testViewController.h //

#import <UIKit/UIKit.h>
@interface testViewController : UIViewController {
IBOutlet UITextField * sourceText;
}
@property(nonatomic,retain) IBOutlet UITextField * sourceText;
-(IBAction) okDoIt;
@end

testViewController.m //

#import "testViewController.h"
#import "MyMethods.h"
@implementation testViewController
@synthesize sourceText;
-(IBAction) okDoIt{
MyMethods * Method = [[MyMethods alloc] init];
NSString * origin = [[NSString alloc] initWithFormat:@"This is the text"];
[Method PrintingText:origin];
[Method release];
[origin release];
}
@end

All connections are well done and tested in IB.

That's all falks! ... :geek:
& sorry for my english!!
Please, help this newbie!!! :mrgreen:


Top 
 Post subject: Re: Newbie!! Please Help me!!!
 Post Posted: Fri Jul 30, 2010 9:03 pm 
Offline
Expert Poster
Expert Poster

Joined: Wed Jul 14, 2010 8:48 am
Posts: 97
Hi Juano,

Ok, you've done some odd things here.

You have an IBOutlet in testViewController for sourceText

You're attempting to utilize that sourceText variable in myMethods - a class that inherited it from testViewController, but in a object your allocating and expect to access a variable in a separate original allocated *parent* object.

Your alloc of myMethods doesn't automagically give it access to sourceText of your original view controller - it will create a WHOLE NEW object with it's own variables - including it's own sourceText. Normally you really don't need to (nor would you probably really want to) subclass your viewController that is already subclassed from UIViewController.

Plus you're attempting to do it the hard way.

All you need to do is link your button to okDoIt

in okDoIt:

NSString * origin = [[NSString alloc] initWithFormat:@"This is the text"];
sourceText.text = origin
[origin release];

Get rid of myMethods entirely, there's no need for it. You can link your okDoIt to the TouchUpInside event of the button.


Top 
 Post subject: Re: Newbie!! Please Help me!!!
 Post Posted: Fri Jul 30, 2010 9:29 pm 
Offline
n00b
n00b

Joined: Fri Jul 30, 2010 3:36 pm
Posts: 4
nethfel, Thank you so much for your help! ...

My question is: There's anyway to use a variable inherited from another subclass? For example:

ViewController Class:

@interface x: UIViewController{
IBOutlet UITextField * Text;
}
-(IBAction) DoSomething;
@end

@implementation x
-(IBAction) DoSomething{
/// USE a function Here ///???
}
@end

// Other class:

@interface Myfunctions
}
-(void) ChangeColor;
@end

@implementation
-(void) ChangeColor{
ZARAZARAZARAZA
Use Text from UIViewController
}
@end

Inside of this last class could be a lot of functions like "ChangeColor" and I need to interact from ViewController class using any object included on.

I can't explain :$ (i'm a fool)

Thank for your time, really!.


Top 
 Post subject: Re: Newbie!! Please Help me!!!
 Post Posted: Fri Jul 30, 2010 11:10 pm 
Offline
Expert Poster
Expert Poster

Joined: Wed Jul 14, 2010 8:48 am
Posts: 97
Is there a reason why you want to do that? remember one of the key things in OO programming - IS A and HAS A.

X IS A view controller
myFunctions IS NOT A view controller

but it could be said X HAS A value manipulator to set colors - and this would be your myFunctions, something that takes some values does some magic and returns a value to be passed back to the view controller to actually set the values of the object being manipulated.

If you wanted myFunctions to manipulate data from X, X could HAS A myFunctions, and then you could call in X say:

psuedo code:

-(IBAction) pressButton:(id) sender {
[self.textEntry setTextColor: [myFunctionsObject setColor:someTestValue]];
}


and say myFunctions might have a method:

-(UIColor *)setColor: (NSString *)colorToSet {
// do a string comparison to find what colorToSet is
// return a autorelease UIColor object with the appropriate values
}

There should be no need to subclass your viewcontroller class. There is no IS A relationship between setting the XYZ of an item in a viewcontroller because the view controller HAS A on all of those items (whether they be buttons, text boxes, image views, etc.)

The view controller itself should be in control of the view, not a secondary subclass of your subclass of a view controller.

Remember - you're working in the MVC paradigm,

Model is your Data - it only is meant to manipulate the data and return values to the Controller

Controller is your in-between - it interacts directly with the view and the model

View is your display - it only handles displaying objects on the screen. It will interact with the view controller.

To do what you're attempting - subclass a subclass of a view is (at least to me) a wasted effort. You're overcomplicating something that doesn't need to be.

Either create a method within your existing view controller subclass OR create another class and either create objects on the fly of it, make a singleton out of it or make an IVAR within your view controller of that object. At that point, that object - and it's related methods - would be available to all methods of your view controller. Just remember to not attempt to sit there and manipulate the actual buttons within the view - that's the view controllers job - just use your other object to manipulate data and return it to the viewcontroller to actually set the objects in the view.


Top 
 Post subject: Re: Newbie!! Please Help me!!!
 Post Posted: Sat Jul 31, 2010 10:09 am 
Offline
n00b
n00b

Joined: Fri Jul 30, 2010 3:36 pm
Posts: 4
Nethfel, Thank you so much, again! ...

I will try with other testes to fix my ideas! ... I'm newbie on Objective-c & iPhone Dev.

Thanks!!! ;)


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 5 posts ] 




Board index » Programming » Objective-C / iPhone Development


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: