开发者

How to call appdelegete method in different class in iphone

开发者 https://www.devze.com 2023-03-16 08:06 出处:网络
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 th开发者_开发技巧em. Is there a way I can av

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 th开发者_开发技巧em. 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]);
    }
}


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];


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];


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

You just need to change the minus for +

+(void)createEditableCopyOfDatabaseIfNeeded;


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号