在Objective-C中,自定义通知只是一个简单的NSString,但在WWDC版本的Swift 3中它并不明显.
nsnotifications nsnotification nsnotificationcenter swift swift3
基本上,我有一个view1,在某些时候,调用view2(via presentModalViewController:animated:).当UIButton按下某个视图2时,view2在view1中调用一个通知方法,然后立即关闭.通知方法会弹出警报.
通知方法工作正常,并被适当调用.问题是,每次创建view1时(一次只能存在一个view1),我可能会NSNotification创建另一个view1因为如果我从view0(菜单)转到view1,然后来回几次,我得到一个一系列相同的警报消息,一个接一个地从通知方法中多次打开一个view1.
这是我的代码,请告诉我我做错了什么:
View1.m
-(void) viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showAlert:)
name:@"alert"
object:nil];
}
-(void) showAlert:(NSNotification*)notification {
// (I've also tried to swap the removeObserver method from dealloc
// to here, but it still fails to remove the observer.)
// < UIAlertView code to pop up a message here. >
}
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
View2.m
-(IBAction) buttonWasTapped {
[[NSNotificationCenter defaultCenter] postNotificationName:@"alert"
object:nil];
[self dismissModalViewControllerAnimated:YES];
}
-(void) dealloc { …Run Code Online (Sandbox Code Playgroud) 在尝试从显示获取数据的视图控制器中取消模型时,在异步提取完成时,我发布了NSNotification.
[[NSNotificationCenter defaultCenter] postNotificationName:@"foobarFetchSuccess" object: foo];
Run Code Online (Sandbox Code Playgroud)
我养成了使用的习惯:
#define FOO_FETCH_SUCCESS @"foobarFetchSuccess"
Run Code Online (Sandbox Code Playgroud)
在一个公共头文件中,然后将其用于addObserver:和removeObserver:以及postNotificationName:
[[NSNotificationCenter defaultCenter] addObserver:self @selector(gotData)
name:FOO_FETCH_SUCCESS object: baz];
Run Code Online (Sandbox Code Playgroud)
所以@"foobarFetchSuccess"字符串遍布整个地方.还有更多像他一样的人.那么一次声明一个字符串并在任何地方使用它的最佳方法是什么?
当键盘弹出UITextfield放置在键盘上时,我正试图将视图向上移动UIScrollView.我使用的是UIKeyboardWillShowNotification与UIKeyboardWillHideNotification此.
当iOS Keyboard我使用高度的地方时,它工作得很好297.
我的客户正在使用Gboard keyboard,他抱怨说观点不动.当我测试时,我的键盘高度为44.
我已经试过这两个键UIKeyboardFrameBeginUserInfoKey和UIKeyboardFrameEndUserInfoKey为NSNotifiction userInfo对象.两者44都只给予.
我试着用UIKeyboardDidShowNotification和UIKeyboardDidHideNotification也,还是同样的问题.
任何人都可以帮我这个..?
我的问题是关于添加到-postNotificationName:object: userInfo:方法的对象.
NSNotification是否保留对象? (与NSMutableDictionary或Array类似)...意味着我可以在发布通知后释放该对象
下面是一个代码片段,用于帮助描述我的问题......释放对象是否有效.Apple文档的链接可能非常有用.
NSMutableDictionary *teamDictCopy = [self.teamDict mutableCopy];
[teamDictCopy setObject:[NSNumber numberWithInt:self.scrollViewIndex] forKey:@"imageIndex"];
if([self.statusButton.title isEqualToString:@"Completed"]){
[[NSNotificationCenter defaultCenter] postNotificationName:@"UnComplete" object:teamDictCopy userInfo:nil];
}
[teamDictCopy release];
Run Code Online (Sandbox Code Playgroud) 由于NSNotificationCenter.defaultCenter().postNotificationName userinfo只接受符合AnyObject协议的数据的字典,有没有人有任何建议如何发布结构作为NSNotification的一部分?
我最初的想法是将结构包装在一个类中 - 但是那么首先使用结构的意义何在.
我是否遗漏了某些内容,或者这只是将Swift与为Objective C构建的API混为一谈的结果?
这是我所描述的一个示范: -
class wrapper: NSObject {
var aStructToWrap: aStruct
init(theStruct: aStruct) {
aStructToWrap = theStruct
super.init()
}
}
struct aStruct {
var aValue: String
}
let aRealStruct = aStruct(aValue: "egg")
NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": aRealStruct]) // ERR: Extra argument 'userinfo' in call
let wrappedStruct = wrapper(theStruct: aRealStruct)
NSNotificationCenter.defaultCenter().postNotificationName("aKey", object: nil, userInfo: ["anotherKey": wrappedStruct]) // no error
Run Code Online (Sandbox Code Playgroud) 我通常使用NSNotification,如下面的示例:
在viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(foo:) name:kName1 object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bar:) name:kName2 object:nil];
Run Code Online (Sandbox Code Playgroud)
在viewDidUnload和dealloc中:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)
但是一位朋友告诉我,我不应该使用,[[NSNotificationCenter defaultCenter] removeObserver:self];因为它会删除所有观察者,包括超类.他建议我使用以下代码逐个删除观察者.
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName1 object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:kName2 object:nil];
Run Code Online (Sandbox Code Playgroud)
我检查了ASIHttpRequest库的代码(https://github.com/pokeb/asi-http-request).它遵循我朋友的建议.
我想知道我的朋友是否正确?在我看来,由于当前实例将是卸载或dealloc,超类的通知也是无用的.并且是否有任何系统UIViewController子类使用通知?
如何检查UIViewController当前是否正在显示?
我UIViewControllers正在倾听NSNotifications- 即使它们没有显示(即未显示).所以,我可以有10个UIViewController在背景中观察NSNotifications的NSNotificationCenter.当a NSNotification发布和接收时UIViewController,我想知道它是否正在显示.如果不是,我将设置一个布尔值,以便在显示视图时处理它.如果它当前正在显示,我会做更多的事情,比如立即更新表,等等......
objective-c uiviewcontroller nsnotification nsnotificationcenter ios
我试图在键盘出现/更改时在键盘上方移动UITextView.假设我显示英文键盘,然后直接切换到中文键盘(比标准英文键盘高).在这种情况下,我的文本视图总是显得太高(肉眼看来,文本视图被中文键盘"输入附件视图"的大小错误地偏移.我发布的图片却缺乏声誉所以:)).当我的应用程序收到UIKeyboardDidShowNotification(使用UIKeyboardFrameEndUserInfoKey获取高度)时,我正在调整文本视图位置,经过一些调查后,UIKeyboardDidShowNotification被多次调用,通常是键盘尺寸不正确(我已经NSLogged了userInfo字典).我在ViewWillAppear中注册我的键盘通知,并在ViewWillDisappear中取消注册.我无法确定导致此通知多次触发的原因; 我的理解是,只有在键盘完成显示后才会触发此通知一次.另外需要注意:我在响应UIKeyboardDidShowNotification的方法中使用了NSLogged"self",它实际上始终是相同的View Controller对象.
但是,即使多次触发此通知,我仍然不明白为什么某些通知的键盘高度会有所不同.其中一个通知始终具有正确的高度,但是当它不是最后一个通知时,文本视图最终会出现在错误的位置.任何有关如何进一步排除故障的见解将非常感激!
编辑:我测试的越多,特别是中文键盘的问题就越多.每当我将键盘从英文切换到中文时,我得到三个UIKeyboardWillShowNotifications:
2014-12-24 22:49:29.385 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 252}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 316}, {320, 252}}";
}
2014-12-24 22:49:29.408 Example[1055:421943] info dictionary: {
UIKeyboardAnimationCurveUserInfoKey = 0;
UIKeyboardAnimationDurationUserInfoKey = 0;
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 442}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}"; …Run Code Online (Sandbox Code Playgroud) 有人能解释一下NSNotificationCenter的重要性吗?
在哪里使用它们?
NSNotificationCenter与AppDelegate有什么区别?
nsnotification ×10
ios ×6
objective-c ×4
iphone ×3
swift ×2
uikeyboard ×2
ipad ×1
struct ×1
swift3 ×1
userinfo ×1