我应该在哪里以及如何在我的项目中编写我的函数(方法)

时间:2014-09-14 23:56:18

标签: ios function

我是iOS开发的新手。 2 - 3年前,我对C编程有一些诅咒,这是我唯一的基础。 我目前正在玩Xcode,直到现在都没有大问题,我有一个简单的问题(但可能太基本了,因为我找不到答案):

在HPViewController.m中,我的所有代码都在“viewDidLoad”下,我想显示几条警告信息,所以我说“好吧,我会在另一个文件中创建一个新函数(不在HPViewController中,因为我想要保持清洁我的代码),这将能够显示一条消息,并从我的HPViewController代码中调用此函数“。 最简单的方法是什么?我在网上发现了很多关于创建新类的事情,但我现在不想创建类和对象:我只想执行一个来自另一个文件的函数(是的,“方法”,抱歉)。 不能少,不再(现在)。这个方法有两个输入(参数):message和message title(所以字符串变量)。

我尝试了很多东西,但我总是编译错误或非工作代码:( 我是从iOS开始的,所以请慢慢解释一下。 谢谢!

(如果我的英语不清楚,我也很抱歉,这不是我的语言:))

--------------------- MORE PRECISION ------------------------ -

这是我的代码:

HPViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface HPViewController : UIViewController <MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UILabel *labelPosition;

@end

HPViewController.m

- (void)viewDidLoad
{
    NSString *Msg;
    NSString *Title; // Global variables - I know, it's bad ! That's why i want to change this !

    Msg = @"Hello World !";
    Title = @"A Message !";

        [self showMessage];
}

- (IBAction)showMessage
{
        UIAlertView *PopUp = [[UIAlertView alloc]
                              initWithTitle:[NSString stringWithFormat:@"%@", Title] message:[NSString stringWithFormat:@"%@", Msg] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [PopUp show];

}

我想要的东西:带有两个输入的新功能,发送“消息”和“标题”并显示消息。

我尝试了什么

  1. 创建新类“MessageManager”
  2. 添加 HPViewController.h

    #include "MessageManager.h"
    
  3. 添加 MessageManager.m (在实施中):

    - (void)showMessage:(NSString *)A withString:(NSString *)B
        {
                UIAlertView *PopUp = [[UIAlertView alloc]
                                      initWithTitle:[NSString stringWithFormat:@"%@", 
    A] message:[NSString stringWithFormat:@"%@", B] 
    delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [PopUp show];
        }
    
  4. MessageManager.h

    #import <Foundation/Foundation.h>
    @interface MSG : NSObject
    - (void)showMessage:(NSString *)Msg AndInfos:(NSString *)Title;
    @end
    
  5. 从HPViewController中删除IBAction,然后替换

    [self showMessage]; 
    
  6. 通过

        sowMessage (LeMsg, TitreDuMsg);
    

    和其他东西,但它根本不起作用

0 个答案:

没有答案