开发者

how to call a method from another class method which consists of IBAction method

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 cla开发者_如何学JAVAss.

thanks a lot in advance...


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";

 }  


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";


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.


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";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜