3

Actually i am testing a sample application in iPhone...where in user interface if i press a button "hello" message should display on text field...such a way that when button pressed it should call a method present in another class i.e, from class2 and display a message on UI so Please help me.... the following code is given below i have two classes say class1 and class2... in class1 i am calling

class1.h

@interface class1 : UIViewController {

      IBOutlet UILabel *statusText;    


}

@property (nonatomic,retain)UILabel *statusText;

- (void)buttonpressed:(id)sender;

@end

class1.m

@implemenation class1

-(IBAction)sayhello:(id)sender

{
class1ViewController *class = [[class1ViewController alloc]init];

    [class hello];
}

class2.h

@interface class2 : UIViewController {

    UILabel *statusText;
}

@property(nonatomic,retain)UILabel *statusText;


-(void)sayhello:(id)sender;


@end

class2.m

@implementation class2

-(void)sayhello:(id)sender;
{

    NSString *newtext = [[NSString alloc] initWithFormat:@"hello"];

    statusText.text = newtext;

    [newtext release];
 }   

Please suggest what changes I should make in the code so that when I press a button it should display the message "HELLO" by calling a method in another class.... I have taken two classes.. I have used IBAction methods... I want to call a method using another class.

thanks a lot in advance...

vakio
  • 3,134
  • 1
  • 22
  • 46
Abhilash
  • 638
  • 4
  • 11
  • 28
  • so let me get this straight you want to call the method present in the class 2 inside the class 1 right is that what you wana do – Radix Jun 01 '11 at 12:50
  • A few bits of advice. 1) Class names should always start with a capital letter. `Class1` `Class2`. 2) `UIViewController` classes should only be used for full-screen display. If you have a text field and a button on the screen simultaneously, they should usually be accessed via the same view controller. 3) Use the `@"..."` construct to create strings. `statusText.text = @"hello";`. It's a lot cleaner. – kubi Jun 01 '11 at 13:10
  • References for [naming conventions](http://cocoadevcentral.com/articles/000082.php), [view controllers](http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/AboutViewControllers/AboutViewControllers.html), [creating strings](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html) – kubi Jun 01 '11 at 13:15
  • plz make your self clear whether you want to know how to show something in view or you want to know how to call a method in another class, or probably, both? – CarmeloS Jun 01 '11 at 13:23

4 Answers4

2

Instead of [class hello], use [class sayhello:nil].

A few notes: the names of classes should begin with a capital letter, e.g. Class1 and Class2. Also, buttonpressed: passes a sender, you are however not using it. It also works when you remove the :(id)sender. And when you set the text of statusText, you create a new string first. This does the same: statusText.text = @"hello";

fabian789
  • 8,348
  • 4
  • 45
  • 91
2

Hi are you aware of writing this

class1ViewController *class = [[class1ViewController alloc]init];

I think it should be:-

-(IBAction)sayhello:(id)sender

{
class2 *class = [[class2 alloc]init];
   NSString *string= [class sayhello];
    [class release];
label.text=string;
}

dont forget to import class2 in class1

in class2 simply write:-

-(NSString)sayhello
{
    return @"hello";

 }  
Gypsa
  • 11,230
  • 6
  • 44
  • 82
1

Hope this helps

class2 * _class2Object = [[class2 alloc]init];
[_class2Object sayHello];

and dont forget to import class2 in class1 else app may crash, since you want to call the method of class2 in class1 so import the class2.h header in class1 and add the above code.

Radix
  • 3,639
  • 3
  • 31
  • 47
0

theoretically, since you are calling a object method(I suppose you know what is the difference between a object method and a class method), so you should alloc and initiate a object of the class which contains the method you want to call(here class2).

I suggest that you display some string made by class2 in class1's view:

so in class1:

-(IBAction)sayHello
{
class2* c2 = [[class2 alloc] init];
statusText.text = [c2 sayhello];
[c2 release];
}

in class2:

-(NSString*)sayhello
{
return @"a string";
}
CarmeloS
  • 7,868
  • 8
  • 56
  • 103
  • tel me difference between object method and class method...? – Abhilash Jun 02 '11 at 04:34
  • @Abhilash to call a object method, you must have a instance of a class, like [c2 sayhello]; above. To call a class method, you don't have to have a instance of a class, if class2 has a class method named singHello, you can call like this: [class2 singHello]; – CarmeloS Jun 02 '11 at 07:19
  • @Abhilash check this question's answer:http://stackoverflow.com/questions/1053592/objective-c-class-vs-instance-methods – CarmeloS Jun 02 '11 at 07:19