如何在iPhone中的不同类中调用appdelegte方法
我有一个有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先在您的视图控制器中创建一个委托变量
,然后您可以调用您在委托中定义的任何方法
就像
[appDelegate方法名称];
in your view controller first of all create a delegate variable
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:
Then in your code using it as so:
将它们设置为公共,这样你就可以用 [ ] 调用它们,
你只需要把 + 的减号改为
Set them as public, so you can call them with [ ]
You just need to change the minus for +
您需要为此类定义一个协议,并将该协议的变量添加到此类的成员变量中,如下所示:
创建对象的类可以使用该对象调用此方法。最好的选择是使用应用程序委托类来实现这些方法。
然后,您可以将对象的委托指定为应用程序委托并调用方法。
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.
首先为
appdelegate
创建一个公共实例。否则,在
constant.h
文件中创建一个实例,然后只需导入 constant.h ,您就可以在任何地方使用
mAppdelegate
,这样您就可以轻松调用first create a common instance for
appdelegate
.otherwise in
constant.h
file create a instance likethen just import constant.h and you may use
mAppdelegate
anywhere so using this you easily call