如何访问或调用函数中的操作

发布于 2024-12-21 10:16:57 字数 2612 浏览 2 评论 0原文

我有正确的导航栏按钮操作,我已经为它设置了一个操作,如下所示:

-(IBAction)save:(id)sender
{
    //Code for saving entered data

    UITextField *fieldOne = [self.fields objectAtIndex:0];
    UITextField *fieldTwo = [self.fields objectAtIndex:1];
    UITextField *fieldThree = [self.fields objectAtIndex:2]; 

    sqlite3_stmt *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
    {

        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name, event, date) VALUES (\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);

        NSLog(@"%@",fieldOne.text);
        NSLog(@"%@",fieldTwo.text);
        NSLog(@"%@",fieldThree.text);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"\n Reminder Saved." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [alert show];
            [alert dismissWithClickedButtonIndex:0 animated:YES];
            [alert release];
        } 

        else 
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Reminder not saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

        rowID = sqlite3_last_insert_rowid(remindersDB);
        NSLog(@"last inserted rowId = %d",rowID);

        sqlite3_finalize(statement);
        sqlite3_close(remindersDB);

    }

    //Alert for not entering any data

    if ([textField.text length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Fill Data" message:@"Please fill name,event and date of reminder" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

    ++self.numberOfSaves;
    NSLog(@"Number of saves = %d",numberOfSaves);

    [self.tableView reloadData];

}

Now I want to check how many times the button is clicked,I have an idea in mind that to create a function and used the condition 

    -(int)numberOfSaves
    {

    int saves = 0;

    if(self.navigationItem.rightBarButtonItem.enabled==YES)
    {

          saves++;

    }
       return saves;
    }

它不起作用......

是否有无论如何检查这样的条件,如果是这样我怎样才能实现它,我是新手Objective-C,请帮帮我......

提前感谢大家:)

I have the right navigation bar button action,I have set an action for it like the following:

-(IBAction)save:(id)sender
{
    //Code for saving entered data

    UITextField *fieldOne = [self.fields objectAtIndex:0];
    UITextField *fieldTwo = [self.fields objectAtIndex:1];
    UITextField *fieldThree = [self.fields objectAtIndex:2]; 

    sqlite3_stmt *statement;
    const char *dbpath = [databasePath UTF8String];

    if (sqlite3_open(dbpath, &remindersDB) == SQLITE_OK && textField.text != nil)
    {

        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO reminders(name, event, date) VALUES (\"%@\", \"%@\", \"%@\")", fieldOne.text, fieldTwo.text,fieldThree.text];

        const char *insert_stmt = [insertSQL UTF8String];

        sqlite3_prepare_v2(remindersDB, insert_stmt, -1, &statement, NULL);

        NSLog(@"%@",fieldOne.text);
        NSLog(@"%@",fieldTwo.text);
        NSLog(@"%@",fieldThree.text);

        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"\n Reminder Saved." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [alert show];
            [alert dismissWithClickedButtonIndex:0 animated:YES];
            [alert release];
        } 

        else 
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"Reminder not saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            [alert release];
        }

        rowID = sqlite3_last_insert_rowid(remindersDB);
        NSLog(@"last inserted rowId = %d",rowID);

        sqlite3_finalize(statement);
        sqlite3_close(remindersDB);

    }

    //Alert for not entering any data

    if ([textField.text length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Fill Data" message:@"Please fill name,event and date of reminder" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }

    ++self.numberOfSaves;
    NSLog(@"Number of saves = %d",numberOfSaves);

    [self.tableView reloadData];

}

Now I want to check how many times the button is clicked,I have an idea in mind that to create a function and used the condition 

    -(int)numberOfSaves
    {

    int saves = 0;

    if(self.navigationItem.rightBarButtonItem.enabled==YES)
    {

          saves++;

    }
       return saves;
    }

It's not working......

Is there anyway to check such condition,If so how can I achieve it,I am newbie to objective-c,please help me out...

Thanks all in advance :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-12-28 10:16:57

您无法从 void 方法返回。

如果这是正确的处理方式,您可能希望将 -(void)numberOfSaves 更改为 -(int)numberOfSaves,但我认为这不是就是你想要的。

在头文件 (.h) 中,您需要为该类声明一个实例变量:

@property (nonatomic, strong) NSInteger numberOfSaves;

在实现文件 (.m) 中,确保综合它:

@synthesize numberOfSaves = _numberOfSaves;

替换您的 IBAction 方法:

- (IBAction)save:(id)sender
{
    ++self.numberOfSaves;
}

然后抛出一起删除 -(void)numberOfSaves 方法。这应该可以完成我认为你想要做的事情。您可以使用 self.numberOfSaves 计算出您现在已提交的保存次数

You cannot return from a void method.

You'd want to change -(void)numberOfSaves to -(int)numberOfSaves, if that were the right way to go about things, but I don't think this is what you want.

In your header file (.h) you'll want to declare an instance variable for the class:

@property (nonatomic, strong) NSInteger numberOfSaves;

In your implementation file (.m) make sure to synthesize it:

@synthesize numberOfSaves = _numberOfSaves;

Replace your IBAction method:

- (IBAction)save:(id)sender
{
    ++self.numberOfSaves;
}

And just throw away the -(void)numberOfSaves method all together. This should accomplish what I think you're trying to do. You can figure out how many saves you have committed now with self.numberOfSaves

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文