类名为:LanguageController
.h文件: #import <Foundation/Foundation.h> #define LANGUAGE_STATE_NOTIFICATION @"LANGUAGE_STATE_NOTIFICATION" #define LANGUAGE_STATE @"LANGUAGE_STATE" @protocol LanguageControllerDelegateNSObject> @required @optional -(voID)updateLanguage; @end @interface LanguageController : NSObject{ IDLanguageControllerDelegate> _delegate; } @property(nonatomic,assign) ID> delegate; +(LanguageController *)languageController; -(voID)setDelegate:(ID>)delegate; @end .m文件: #import "LanguageController.h" static LanguageController *languageController =nil; @implementation LanguageController @synthesize delegate = _delegate; +(LanguageController *)languageController { @synchronized(self){ if (languageController == nil) { languageController = [[LanguageController alloc] init]; } } return languageController; } -(voID)setDelegate:(ID>)delegate{ _delegate = delegate; [[NSNotificationCenterdefaultCenter] addobserver:selfselector:@selector(languageChanged:)name:LANGUAGE_STATE_NOTIFICATIONobject:nil]; } -(voID) languageChanged:(ID) sender{ if(_delegate !=nil && [_delegate respondsToSelector:@selector(updateLanguage)]) { [_delegateupdateLanguage]; } } - (voID)dealloc { [[NSNotificationCenterdefaultCenter] removeObserver:selfname:LANGUAGE_STATE_NOTIFICATIONobject:nil]; _delegate=nil; [superdealloc]; } @end
将以上两个文件加入到工程后,使用时就非常方便了,首先在更改语言的按钮事件中添加语言切换通知语句:
[[NSNotificationCenter defaultCenter] postNotificationname:LANGUAGE_STATE_NOTIFICATION object:nil];
然后在所有需要刷新页面的vIEwController的vIEwDIDLoad函数中加入设置代理的语句:
[LanguageController languageController].delegate = self;
再在这些vIEwController类中实现LanguageControllerDelegate的方法-(voID)updateLanguage;所有因语言切换而需要更新或刷新的工作都放着这个代理方法中实现即可。
好了,看完你只要将上面的.h和.m文件加入到自己的工程中,再加上上面提到的几行代码,就可以了,是不是很方便呢
总结以上是内存溢出为你收集整理的iphone ipad 开发应用内实现多语言自由切换全部内容,希望文章能够帮你解决iphone ipad 开发应用内实现多语言自由切换所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)