I am currently using NSNotificationCentre to pass the WebViewStart and WebViewFinish events for 5 WebViews.
Inside the WebViewStart method I start the animation of a progress bar.
Inside the WebViewFinish method I stop the animation of a progress bar.
Obviously the problem is that if 5 WebViews are loading, and one WebView finishes loading, it fires the WebViewFinish method and stops the animation, even though the other WebViews are still loading.
Is there any way to check something like the following?
- (void)_webViewProgressFinished:(NSNotification *)notification
{
if ([webView1 & webView2 & webView3 finishedLoading]) {
[_loadingIndicator stopAnimation:self];
}
}
The code I have at the moment doesn't seem appropriate for the number of WebViews I have. The code I am using at the moment and having problems with is as follows:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_mainWebView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_mainWebView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView1];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView1];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView3];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView3];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView4];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView4];
}
- (void)_webViewProgressStarted:(NSNotification *)notification
{
[_loadingIndicator startAnimation:self];
}
- (void)_webViewProgressFinished:(NSNotification *)notification
{
[_loadingIndicator stopAnimation:self];
}
I hope someone can help. Thanks in advance everyone!
EDIT: I ended up finding the solution myself. May not be the most elegant, but nevertheless:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_mainWebView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_mainWebView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView1];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView1];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView2];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView3];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView3];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressStarted:) name:WebViewProgressStartedNotification object:_subWebView4];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(_webViewProgressFinished:) name:WebViewProgressFinishedNotification object:_subWebView4];
}
- (void)_webViewProgressStarted:(NSNotification *)notification
{
if ([_mainWebView isEqual:[notification object]]) {
[_mainWebViewProgress startAnimation:self];
} else if ([_subWebView1 isEqual:[notification object]]) {
[_subView1Progress startAnimation:self];
} else if ([_subWebView2 isEqual:[notification object]]) {
[_subView2Progress startAnimation:self];
} else if ([_subWebView3 isEqual:[notification object]]) {
[_subView3Progress startAnimation:self];
} else if ([_subWebView4 isEqual:[notification object]]) {
[_subView4Progress startAnimation:self];
}
}
What this does is gets the notification object, which is an ID, and compares it to our WebViews. If they are the same, then that WebView has started/finished load.
Hope this helps anyone.
Create a separate notification for each of the 5 web views. Create 5 boolean values that can be set to true as each web view is finished. When a web view finishes, have the notification center post it's notification. The method that receives this notification should first set it's boolean value to true saying that it finished. Then check to see if all 5 boolean values are set to true. If yes, stop the activity indicator. If no, leave it spinning.
You could just count until you get 5 notifications. you could also estimate progress as the views finish one at a time, Nx20% for the aggregate in percentage.
Related
This is my AlertView code :
- (void)initializeAlertControllerForOneButtonWithTitle:(NSString *)title withMessage:(NSString *)msg withYesButtonTitle:(NSString *)yesButtonTitle withYesButtonAction:(id)yesButtonAction
{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:title
message:msg
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesBtn = [UIAlertAction
actionWithTitle:yesButtonTitle
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (self.activityIndicator.animating) {
[self.activityIndicator stopAnimating];
}
if ([title isEqualToString:#"Wrong Password!"]) {
self.editTextField.text = #"";
[self.editTextField becomeFirstResponder];
}
}];
[alert addAction:yesBtn];
[self presentViewController:alert animated:YES completion:nil];
}
I am trying to fire this alert in my NSNotificatoin Response method. My Notification Response code :
- (void)receiveSMSVerificationResponse:(NSNotification *)notification
{
SMSVerificationDigitClassModel *smsVerificationDigitClassModel = [[SMSVerificationDigitClassModel alloc] init];
smsVerificationDigitClassModel = [notification object];
if (smsVerificationDigitClassModel.viewControllerName == ViewControllerNameProfileInfoEditViewController) {
if ([self alreadyRegisteredPhoneNumber:smsVerificationDigitClassModel.phoneNumber] == YES) {
NSLog(#"jogajog");
[self initializeAlertControllerForOneButtonWithTitle:#"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:#"Ok" withYesButtonAction:nil];
} else {
if ([AdditionalClasses internetConnectionCheck] == YES) {
self.userModelClass.phone_number = smsVerificationDigitClassModel.phoneNumber;
[self updateUserModel:self.userModelClass];
} else {
[self noInternetConnectionAlert];
}
}
//Check if that phone number is already used
// udate phone numner in server
// update phone number in core data
//[self goToSignUpViewControllerWithPhoneNumber:smsVerificationDigitClassModel.phoneNumber];
}
}
I check it from break point, this line [self initializeAlertControllerForOneButtonWithTitle:#"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:#"Ok" withYesButtonAction:nil]; is actually calling, but the alert view is not popping up. It says:
"Warning: Attempt to present on whose view is not in the window hierarchy!"
I have tried to add notification observer methods :
- (void)addNotificationObserver
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveUserModelResponse:) name:#"sendUpdateRequestToServerForPhoneNumberWithUserModel" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveSMSVerificationResponse:) name:#"SMSVerificationForPhoneNumber" object:nil];
}
In viewDidLoad, viewDidAppear & in viewWillAppear and removeObserver in dealloc,
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"sendUpdateRequestToServerForPhoneNumberWithUserModel" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"SMSVerificationForPhoneNumber" object:nil];
}
but it is not showing at all. So, how do I change my window hierarchy! in this viewController. If you understand, please reply back. A lot of thanks in advance.
Call the initializeAlertControllerForOneButtonWithTitle method inside a main queue dispatch queue block.
All UI operation should be on main threat.
dispatch_async(dispatch_get_main_queue(), ^{
[self initializeAlertControllerForOneButtonWithTitle:#"Already Registered!" withMessage:kAlreadyRegisteredPhoneNumberMSGForChangePhoneNumber withYesButtonTitle:#"Ok" withYesButtonAction:nil];
});
UIKeyboard will show and hide notification method working fine in iOS8 and later, but it not works in iOS7. Is there any alternative?
My Application Deployment target is iOS7.
My Code is here
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(HideKeyboard:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWasShown:(NSNotification *)sender
{
CGSize kbSize =
[[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
if (!scroll) {
scrollValue = self.WholeScreenUIView.frame.origin.y - kbSize.height;
}
scroll = YES;
}
- (void)HideKeyboard:(NSNotification *)sender
{
scroll = NO;
scrollValue = 0.0;
}
Thanks in advance.
Use this code..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHideHandler:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillshowHandler:)
name:UIKeyboardWillShowNotification
object:nil];
- (void) keyboardWillHideHandler:(NSNotification *)notification {
[scroll setContentOffset:CGPointMake(0, 0) animated:YES];
}
- (void) keyboardWillshowHandler:(NSNotification *)notification {
[scroll setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height+44)];
}
Hope this helps.This works in my case
I am adding observer in viewdidload -
[[NSNotificationCenter defaultCenter] removeObserver:self name:aName object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(aMethod) name:aName object:nil];
But every time when i am opening view its adding observer. How to manage this?
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:aName object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(aMethod) name:aName object:nil];
}
I have three methods:
- (void)viewDidAppear:(BOOL)animated
{
[self updateViews];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:#"itemQuantityChanged" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveNotification:) name:[NSString stringWithFormat:#"Item %# deleted", itemUUID] object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) receiveNotification: (NSNotification *)notification
{
if ([[notification name] isEqualToString:#"itemQuantityChanged"])
[self updateViews];
if ([[notification name] isEqualToString:[NSString stringWithFormat:#"Item %# deleted", itemUUID]])
NSLog(#"FAIL!");
}
The main idea is that for this class I need to receive 2 different notifications and in case of receiving them need to perform different actions. Is everything ok with the realization? I believe that it is possible to simplify this code. How to removeObserver correctly? I don't use ARC.
You should use a different selector for each notification. That way, you don't need any logic in the method to determine which notification was sent.
Removing the observers as you are doing is fine.
How can I print every single notification on my system in obj-C?
[[NSNotificationCenter defaultCenter] addObserver:self ...];
what does in "..."? Should I use NSDistributedNotificationCenter?
let's say I have a function called logfunc which will do NSLog(#"ok");
thank you
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod:) name:nil object:nil];
You can then define a method that matches your selector, like this:
- (void)myMethod:(NSNotification *)notification {
NSLog(#"notification received: %#", notification);
}