UIPopoverController isPopoverVisible returns wrong value - ios

if([messageNotification isPopoverVisible])
[messageNotification dismissPopoverAnimated:YES];
I have the above two lines of code in my project where message_Notification is a UIPopovercontroller's object.
The problem is isPopoverVisible returns false when my Popover actually presented on the main viewController. Any idea about this??

Check that message_Notification is available or not and then check for visiblity
And I think there is some inconsistency because messageNotification and message_Notification should be same as I have done in my code
if(messageNotification)
{
if([messageNotification isPopoverVisible])
{
[messageNotification dismissPopoverAnimated:YES];
}
}
else
{
NSLog(#"Not availble");
}

Related

Comparing class is giving incorrect output

I am using below code to check view controllers.
NSLog(#"addProductClicked 1===%#", self.class);
NSLog(#"addProductClicked 2===%#", [CategoriesViewController class]);
if ([self.class isKindOfClass:[CategoriesViewController class]]) {
NSLog(#"you go it right");
} else {
NSLog(#"you go it wrong");
}
The output I get is as below.
addProductClicked 1===CategoriesViewController
addProductClicked 2===CategoriesViewController
you go it wrong
Any idea what is going wrong?
Just to update, below is what I have defined my view controller...
#interface CategoriesViewController : GlobalViewController {
Now in GlobalViewController I have method where I am checking above...
The variable you want to class check should be passed in as an object, not as a class.
if ([self isKindOfClass:[CategoriesViewController class]]) {
NSLog(#"you go it right");
} else {
NSLog(#"you go it wrong");
}
Thats is wrong comparison. You call isKindOfClass: on the object of that class. Something like this:
CategoriesViewController *obj = [[CategoriesViewController alloc] init];
[obj isKindOfClass:CategoriesViewController];
In your case you probably want to put a check on self.

I get no animation when I perform the segue programmatically

I have a button that will segue to another View Controller. In shouldPerformSegueWithIdentifier I check the identifier and returns NO for this certain button after calling my own method nextClicked.
In nextClicked I do a HTTP Request and check some stuff, and then if everything is OK I do
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:#"getstartednext" sender:self];
});
The problem is that I get no animation now... The segue is of type modal. If I return YES in shouldPerformSegueWithIdentifier and don't to the segue programmatically later, the animation appears and everything is as it should.
You are misusing shouldPerformSegueWithIdentifier. If you want to do something special in response to the button click, use action-target and not a segue connected to the button.
I created a new project with your idea and I've got animation.
This is the action connected through IB with the next viewController and with identifier: #"getstartednext"
- (IBAction)go:(id)sender
{
}
Same code you have except sender is nil
- (void) nextClicked
{
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:#"getstartednext" sender:nil];
});
}
if sender != nil, (IBAction go:) then I'm calling nextClicked because returning NO
else the code is doing the segue because returning YES
- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
if ([identifier isEqualToString:#"getstartednext"] && sender != nil)
{
[self nextClicked];
return NO;
}
return YES;
}
Works like a charm.

Check if a Specific Instance of class already exists

is there anyway to check if a specific instance of a class has already been created. I feel like it is hard to check if the instance already exists when there is a chance you may not have created it yet.
Here is my code:
-(IBAction)done:(id)sender
{ //I want to figure out how to check if 'newWindow' already exists before I create another 'newWindow'
SimpleTableView *newWindow = [self.storyboard instantiateViewControllerWithIdentifier:#"SimpleTableView"];
[self.navigationController pushViewController:newWindow animated:YES];
}
Thanks for all the help guys.
Yes, there is a simple way to do it.
You just need to have some reference to it (for example create a property) and check whether it is nil (not initialized) or not. You can do it like this:
if(!myReference){
//if you get here it means that it hasn't been initialized yet so you have to do it
}
First make newWindow an ivar or a property.
Then:
if (!newWindow){
newWindow = [self.storyboard instantiateViewControllerWithIdentifier:#"SimpleTableView"];
}
I wrote you a method that checks all viewControllers in UINavigationController:
- (BOOL)classExistsInNavigationController:(Class)class
{
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:class])
{
return YES;
}
}
return NO;
}
Use it like this:
- (IBAction)done:(id)sender
{
//I want to figure out how to check if 'newWindow' already exists before I create another newWindow
if (![self classExistsInNavigationController:[SimpleTableView class]])
{
SimpleTableView *newWindow = [self.storyboard instantiateViewControllerWithIdentifier:#"SimpleTableView"];
[self.navigationController pushViewController:newWindow animated:YES];
}
}
You can also do something like this:
- (UIViewController *)classExistsInNavigationController:(Class)class
{
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:class])
{
return controller;
}
}
return nil;
}
And use it like this if you want to pop to the viewController that exists already:
- (IBAction)done:(id)sender
{
//I want to figure out how to check if 'newWindow' already exists before I create another newWindow
UIViewController *controller = [self classExistsInNavigationController:[SimpleTableView class]];
if (!controller)
{
SimpleTableView *newWindow = [self.storyboard instantiateViewControllerWithIdentifier:#"SimpleTableView"];
[self.navigationController pushViewController:newWindow animated:YES];
}
else
{
[self.navigationController popToViewController:controller animated:YES];
}
}
You can use if/else to check newWindow exists or not.
if (newWindow) { // newWindow is exist to do something
// Do something.
} else { // newWindow is not exist to do something
// Do something.
}
You can implement an instance counter (https://stackoverflow.com/a/30509753/4647396) in the class you want to track.
Then just check if the counter is greater than 0.
If I interpret your question correctly you just want to know wether an instance exists and dont need a reference to it.

how to get the a property value when dismissing uipopover

I am using a uipopover to present a mini number pad to the user when they enter a textfield on my main view controller.
when they enter numbers using the number pad, i save the entry into a nsstring property that I've named keypadvalue.
there is an unwind segue wired to a done button on the popover which fires the following code.
- (IBAction)doneWithKeyboard:(UIStoryboardSegue *)segue
{
NSLog(#"unwind");
if ([segue.sourceViewController isKindOfClass:[KeyPopupViewController class]])
{
KeyPopupViewController *popOver2 = segue.sourceViewController;
activeField.text =popOver2.keypadValue;
}
}
the activetextfield on my main view controller then gets updated to the kepadvalue, and this all works fine.
my problem now is that i want the activetextfield to update the same way if the user presses outside the uipopover, and it dismisses without firing the unwind segue.
i thought i might use the following to perform the update when the popover dismisses
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
activeField.text = controller.keypadValue;
return YES;
}
unfortunately despite multiple attempts i can't get the property to return a value it is always null even though the method fires as expected.
how should i recover the property value from the popover using this or another method?
i am obviously doing something wrong
can anyone advise
thanks
It should help:
-(BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
[self.view endEditing:YES];
activeField.text = controller.keypadValue;
return YES;
}

can autoresizemask be applied on popovers

Again caught up in this orientation issue. I have a search screen where when a user taps on textfields a popover is displayed below that just like a dropdown menu but when the orienattion is changed to ladscape popovers appear a bit to the left .
Can this be handled(apart from managing coordinates)
Thanks in advance
tried this piece of code
- (void)willRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (isPopOverNeedToBeManaged) {
NSLog(#"Popover need to be managed");
if (popOverSender) {
NSLog(#"yes text field is there");
if (popoverController) {
[popoverController dismissPopoverAnimated:NO];
}
}
}
}
(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (isPopOverNeedToBeManaged) {
NSLog(#"Popover need to be managed");
if (popOverSender) {
NSLog(#"yes text field is there");
if (popoverController) {
[popoverController presentPopoverFromRect:txtDDDeliveryCenter.frame inView:subview permittedArrowDirections:0 animated:YES];
}
}
}
Watch popover behavior in any Apples native application. You'll see that in response to device rotation popover should disappear before rotation animation and appear again on new appropriate place after it ends. So you should dismiss your popover and then popup it on new place with new coordinates.
Hope that'll help

Resources