如何在iPhone中的不同类中调用appdelegte方法

发布于 2024-11-18 00:01:23 字数 1620 浏览 4 评论 0原文

我有一个有 10 个表的数据库。由于我需要在不同的视图控制器中访问此数据库,因此我必须在每个视图控制器中声明如下所示的两个方法。有没有办法通过在应用程序委托中声明这些方法来避免这种情况。如果是,我该如何在不同的类中使用这些方法。

- (NSString *) getWritableDBPath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:DATABASE_NAME];
}

-(void)createEditableCopyOfDatabaseIfNeeded 
{
    // Testing for existence
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
    NSLog(@"%@",writableDBPath);

    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return;

    // The writable database does not exist, so copy the default to
    // the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
                               stringByAppendingPathComponent:DATABASE_NAME];
    success = [fileManager copyItemAtPath:defaultDBPath
                                   toPath:writableDBPath
                                    error:&error];
    if(!success)
    {
        NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
                  [error localizedDescription]);
    }
}

I have a database with 10 tables. As I need to access this database in different view controllers, I have to declare the two methods shown below in each of them. Is there a way I can avoid this by declaring these methods in the application delegate. If yes, how can I go about using these methods in different classes.

- (NSString *) getWritableDBPath {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:DATABASE_NAME];
}

-(void)createEditableCopyOfDatabaseIfNeeded 
{
    // Testing for existence
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
    NSLog(@"%@",writableDBPath);

    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return;

    // The writable database does not exist, so copy the default to
    // the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
                               stringByAppendingPathComponent:DATABASE_NAME];
    success = [fileManager copyItemAtPath:defaultDBPath
                                   toPath:writableDBPath
                                    error:&error];
    if(!success)
    {
        NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
                  [error localizedDescription]);
    }
}

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

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

发布评论

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

评论(5

遗失的美好 2024-11-25 00:01:23

首先在您的视图控制器中创建一个委托变量

YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication]delegate];

,然后您可以调用您在委托中定义的任何方法
就像[appDelegate方法名称];

in your view controller first of all create a delegate variable

YourAppDelegate *appDelegate=(YourAppDelegate *)[[UIApplication sharedApplication]delegate];

then u can call any methods that you have define in your delegate
like [appDelegate methodName];

苦笑流年记忆 2024-11-25 00:01:23

这只是尖叫着作为具有类级别方法的单独控制器来实现。我强烈建议创建一个具有如下定义的数据库控制器:

@interface DatabaseController: NSObject  
    + (NSString *) getWritableDBPath ;
    + (void) createEditableCopyOfDatabaseIfNeeded  ;
@end 

然后在您的代码中使用它,如下所示:

#import "DatabaseController.h"


NSString * somePath = [DatabaseController getWritableDBPath];

[DatabaseController createEditableCopyOfDatabaseIfNeeded];

This just screams to be implemented as a separate controller with class level methods. I would highly recommend creating a Database controller with a definition like so:

@interface DatabaseController: NSObject  
    + (NSString *) getWritableDBPath ;
    + (void) createEditableCopyOfDatabaseIfNeeded  ;
@end 

Then in your code using it as so:

#import "DatabaseController.h"


NSString * somePath = [DatabaseController getWritableDBPath];

[DatabaseController createEditableCopyOfDatabaseIfNeeded];
冬天的雪花 2024-11-25 00:01:23

将它们设置为公共,这样你就可以用 [ ] 调用它们,

你只需要把 + 的减号改为

+(void)createEditableCopyOfDatabaseIfNeeded;

Set them as public, so you can call them with [ ]

You just need to change the minus for +

+(void)createEditableCopyOfDatabaseIfNeeded;
居里长安 2024-11-25 00:01:23

您需要为此类定义一个协议,并将该协议的变量添加到此类的成员变量中,如下所示:

创建对象的类可以使用该对象调用此方法。最好的选择是使用应用程序委托类来实现这些方法。

然后,您可以将对象的委托指定为应用程序委托并调用方法。

@protocol mySqlDelegate ;

@interface mySqlClass  {

    id <mySqlDelegate> delegate;
}
@property (nonatomic, assign) id <mySqlDelegate> delegate;
@end



@protocol mySqlDelegate 

- (void) delegateMethodsForThisClass;

@end

You will need to define a protocol for this class and add a variable of that protocol to the member variable of this class as follows:

The classes where the object is created can either call this method using the object. The Best option is to use the app delegate class to implement these methods.

You can then assign the objects's delegate as the app delegate and call the methods.

@protocol mySqlDelegate ;

@interface mySqlClass  {

    id <mySqlDelegate> delegate;
}
@property (nonatomic, assign) id <mySqlDelegate> delegate;
@end



@protocol mySqlDelegate 

- (void) delegateMethodsForThisClass;

@end
远昼 2024-11-25 00:01:23

首先为 appdelegate 创建一个公共实例。
否则,在 constant.h 文件中创建一个实例

 mAppDelegate=(YourAppDelegate*)[[UIApplication sharedApplication] ];

,然后只需导入 constant.h ,您就可以在任何地方使用 mAppdelegate ,这样您就可以轻松调用

first create a common instance for appdelegate.
otherwise in constant.h file create a instance like

 mAppDelegate=(YourAppDelegate*)[[UIApplication sharedApplication] ];

then just import constant.h and you may use mAppdelegate anywhere so using this you easily call

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