在appDelegate中调用函数

时间:2017-03-02 11:38:46

标签: ios objective-c

    //find all data from database
- (void) findAllSupplement
{
    const char *dbpath = [_databasePath UTF8String];
    sqlite3_stmt    *statement;

    if (sqlite3_open(dbpath, &_contactDB) == SQLITE_OK)
    {
        NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM SupplementInformationTable"];

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(_contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {
            NSMutableArray *allRows = [[NSMutableArray alloc] init] ;

            while(sqlite3_step(statement) == SQLITE_ROW)
            {

                char *field1 = (char *) sqlite3_column_text(statement,0);
                NSString *field1Str1 = [[NSString alloc] initWithUTF8String: field1];

                char *field2 = (char *) sqlite3_column_text(statement,1);
                NSString *field1Str2 = [[NSString alloc] initWithUTF8String: field2];

                char *field3 = (char *) sqlite3_column_text(statement,2);
                NSString *field1Str3 = [[NSString alloc] initWithUTF8String: field3];

                char *field4 = (char *) sqlite3_column_text(statement,3);
                NSString *field1Str4= [[NSString alloc] initWithUTF8String: field4];

                char *field5 = (char *) sqlite3_column_text(statement,4);
                NSString *field1Str5 = [[NSString alloc] initWithUTF8String: field5];

                char *field6 = (char *) sqlite3_column_text(statement,5);
                NSString *field1Str6 = [[NSString alloc] initWithUTF8String: field6];

                char *field7 = (char *) sqlite3_column_text(statement,6);
                NSString *field1Str7= [[NSString alloc] initWithUTF8String: field7];

                char *field8 = (char *) sqlite3_column_text(statement,7);
                NSString *field1Str8 = [[NSString alloc] initWithUTF8String: field8];

                char *field9 = (char *) sqlite3_column_text(statement,8);
                NSString *field1Str9 = [[NSString alloc] initWithUTF8String: field9];

                NSMutableDictionary *dataDictionary=[[NSMutableDictionary alloc]init];
                [dataDictionary setObject:field1Str1 forKey:@"supplementIdentity"];
                [dataDictionary setObject:field1Str2 forKey:@"supplementName"];
                [dataDictionary setObject:field1Str3 forKey:@"supplementDescription"];
                [dataDictionary setObject:field1Str4 forKey:@"offerIdentity"];
                [dataDictionary setObject:field1Str5 forKey:@"offerName"];
                [dataDictionary setObject:field1Str6 forKey:@"offerDescription"];
                [dataDictionary setObject:field1Str7 forKey:@"flavourIdentity"];
                [dataDictionary setObject:field1Str8 forKey:@"imageLink"];
                [dataDictionary setObject:field1Str9 forKey:@"quantityPurchase"];

                // Add the string in the array.
                [allRows addObject:dataDictionary];
                NSLog(@"Match found") ;

                //send all data to the cart container screen
                CartContainer *objCartContainer=[[CartContainer alloc]init];
                objCartContainer.arrayConatinLocalDataFromDB=allRows;
            }
            sqlite3_finalize(statement);
            NSLog(@"%@",allRows);
        }
        sqlite3_close(_contactDB);
    }
}

如何在app委托中调用此函数?此函数在视图控制器中声明并定义,名为productDetail.m。我想为整个应用程序执行此函数。将不胜感激。

1 个答案:

答案 0 :(得分:0)

在Appdelegate.m中编写函数

从viewController.m调用 形成这段代码:

AppDelegate *appDelegate;

appDelegate= (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate findAllSupplement];

或者您可以在全球范围内声明功能。喜欢: 在ViewController.h中

+ (void) findAllSupplement;

在ViewControler.m中定义功能

+ (void) findAllSupplement
{
// your code here
}

你可以从任何一个班级打电话给你。