简体   繁体   English

如何从包含 IBAction 方法的另一个 class 方法调用方法

[英]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 ie, 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实际上,我正在 iPhone 中测试示例应用程序...如果我按下按钮,则在用户界面中的“hello”消息应显示在文本字段上...这样,当按下按钮时,它应该调用另一个 class 中存在的方法,即,来自class2并在UI上显示一条消息,所以请帮助我....下面给出了以下代码我有两个类说class1和class2 ...在class1中我正在调用

class1.h类1.h

@interface class1 : UIViewController {

      IBOutlet UILabel *statusText;    


}

@property (nonatomic,retain)UILabel *statusText;

- (void)buttonpressed:(id)sender;

@end

class1.m类1.m

@implemenation class1

-(IBAction)sayhello:(id)sender

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

    [class hello];
}

class2.h类2.h

@interface class2 : UIViewController {

    UILabel *statusText;
}

@property(nonatomic,retain)UILabel *statusText;


-(void)sayhello:(id)sender;


@end

class2.m类2.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.请建议我应该在代码中进行哪些更改,以便当我按下按钮时,它应该通过调用另一个 class 中的方法来显示消息“HELLO”……我已经上了两门课……我使用了 IBAction 方法…… .我想调用一个使用另一个class的方法。

thanks a lot in advance...非常感谢提前...

Instead of [class hello] , use [class sayhello:nil] .而不是[class hello] ,使用[class sayhello:nil]

A few notes: the names of classes should begin with a capital letter, eg Class1 and Class2 .一些注意事项:类的名称应该以大写字母开头,例如Class1Class2 Also, buttonpressed: passes a sender , you are however not using it.此外, buttonpressed:传递了一个sender ,但是您没有使用它。 It also works when you remove the :(id)sender .当您删除:(id)sender时,它也可以工作。 And when you set the text of statusText , you create a new string first.当您设置statusTexttext时,您首先创建一个新字符串。 This does the same: statusText.text = @"hello" ;这也是一样的: statusText.text = @"hello" ;

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不要忘记在 class1 中导入 class2

in class2 simply write:-在 class2 中只需写:-

-(NSString)sayhello
{
    return @"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.并且不要忘记在class1中导入class2,否则应用程序可能会崩溃,因为您想在class1中调用class2的方法,所以在class1中导入class2.h header并添加上述代码。

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). 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:我建议您在 class1 的视图中显示一些由 class2 制作的字符串:

so in class1:所以在class1中:

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

in class2:在类 2 中:

-(NSString*)sayhello
{
return @"a string";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM