-1

您好 Stackoverflow 的好人,我正在尝试创建一个获取几秒钟的函数,然后创建一个本地警报,这就是我所拥有的:

我创建了一个名为 manualtimer.h 的类,这是一个 Nsobject 类。

在 manualTimer.hi 中有以下代码:

-(void)tempoAlerta:(NSTimeInterval *)otempo;

在 manualTimer.M 我有以下代码:

-(void)tempoAlerta:(NSTimeInterval *)otempo {


    UIApplication* myapp = [UIApplication sharedApplication];
    UILocalNotification* localnote = [[UILocalNotification alloc]init];

    localnote.fireDate = [NSDate dateWithTimeIntervalSinceNow:*otempo];
    localnote.alertBody = @"Nice Alert";
    localnote.timeZone = [NSTimeZone defaultTimeZone];
    NSMutableArray *notifications = [[NSMutableArray alloc] init];
    [notifications addObject:localnote];
    myapp.scheduledLocalNotifications = notifications;

}

在我的视图控制器中,我导入了 manualTimer.h,但由于某种原因无法调用函数 tempoAlerta()

有任何想法吗?xCode 显示没有错误?

4

2 回答 2

0

您必须实例化该类的对象

manualTimer *obj = [[manualTimer alloc] init];

然后调用该函数

[obj tempoalerta:otempo];
于 2013-09-11T21:54:08.903 回答
0

您在示例代码中所拥有的是定义一个方法而不是一个函数。函数是 c 风格的void anExampleFunction(int anArgument),不能在 Objective-c 类上定义。

您如何调用该方法?

您的电话应如下所示:

ManualTimer *timer = [ManualTimer new];
[timer temoAlerta:10];

请查看此文档以获取更多信息: https ://developer.apple.com/library/ios/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/#//apple_ref/doc/uid/TP40007594-CH1-SW11

于 2013-09-11T21:54:14.783 回答