UIPopover button repeats action - xcode

I am creating the application for iPad with the UIPopover. I have a main view from which I am calling two popovers displaying different information. I was following the guidelines in How to Dismiss a Storyboard Popover thread and another threads, everything works fine except one thing. In my popover I have a button which triggers an action on the parent view. From time to time the action is triggered more than once even if the button was clicked just once and also popover was opened just once. My first assumption was that the popover was caching some data from several calls, but the problem seems to appear just randomly.
My configuration is: Mac OSx Snow Leopard with Xcode 4.2, iOS 5.0.
Tested in Simulator, iPad 5.1 and iPad 6.0 all the same results.
I have the main view View 1 and the popover view View 2
In my view 2 I have a method ProceedButtonClicked which sends the notification to the View 1
- (IBAction) ProceedButtonClicked{
[[NSNotificationCenter defaultCenter] postNotificationName:#"proceedButtonClicked" object:self];
}
The method is binded to the button in the popover view.
In view1 (parent view):
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ButtonClicked:) name:#"proceedButtonClicked" object:nil];
}
- (void) ButtonClicked:(NSNotification *) notification {
NSLog(#"I'm here ...");
//dismiss popover
if (paramPopover)
[paramPopover dismissPopoverAnimated:YES];
}
I am pretty new to the iPad development, so maybe I am missing in my code something obvious, but searching until now resulted to nothing.
Any help would be appreciated.
Thank you.

When using notifications, you risk multiple instances of a class answering the same notification, so if you have 2 controllers alive for some reason (bad memory management?), then when pressing the button 2 controllers will answer the call and you will have a duplicate action.
Buttons can have a specific callback assigned to them, and it's very simple to set it up by code:
If your button is a UIButton, you can set your target action like this:
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
If your button is a UIBarButtonItem, you set the target when you create it
[[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:self action:#selector(buttonAction:)];
Edit:
NSLog(#"I'm here ...");
That's creepy ...

Related

Update a UITableview from a different view when both views visible at once

Looking at my iPad app, I have several containerviews. Containerviews enable me to show all four iphone views on a single iPad screen simultaneously. One view, called TrackingViewController, has a UITableView on it (called table) that I want to refresh from a button on a second view controller (MainViewController) which is also visible on the same iPad screen. I call the code
TrackingViewController *trackView = [[TrackingViewController alloc] init];
[trackView.view setNeedsDisplay];
[trackView.table reloadData];
at the end of the IBAction for the button in MainViewController, the data transfers perfectly, but the table itself on TrackingViewController does not visually update to show this data.
If I then manually initiate a completely different modal view controller and dismiss it on TrackingViewController when the app is running on iPad then the data shows up. How do I make the view or data on TrackingViewController automatically update visually when the button is pressed on MainViewController?
iPad Simulator may help.
I also dont understand your question..
The only thing I see is, you want to update two UITableViews but in your code you are only reloading Data of one TableView?!?
[trackView.table reloadData];
[otherTable reloadData];
But I realy dont understand your question....
If your other TableView is in another class, just try something like this...
In TrackingViewController.h:
- (void)refreshMyTable;
In TrackingViewController.m:
- (void)refreshMyTable{
[table reloadData];
}
In your MainViewController.m Action, that should refreshes both Tables:
- (IBAction)theRefreshingAction{
TrackingViewController *trackView = [[TrackingViewController alloc] init];
[trackView refreshMyTable]; //This refreshes Table in trackView
[mainViewTable reloadData]; //This in MainView
}
Ended up using NSNotificationCenter by utilizing the following post:
Calling a function from a different view controller for iphone
In my case, calling a function that only included [table reloadData]; wouldn't work, but calling my performFetch function did (which also called [table reloadData]; at the end of it).

iOS6 ScrollBarShouldScrollToTop not firing/ ScrollView Delegate issue

I am adding a dummy ScrollView to my app to detect a user click on the status bar, to performa an event in my program.. I am creating it in the ViewDidLoad:
//Dummy Scroll is for the tap on status bar to work
UIScrollView *dummyScrollView = [[UIScrollView alloc] init];
dummyScrollView.delegate = self;
[[self view ] addSubview:dummyScrollView];
[[self view] sendSubviewToBack:dummyScrollView];
I then implement :
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
NSLog(#"scrollViewShouldScrollToTop");
.
.
}
Under all previous versions of IOS this has worked beautifully and flawlessly, yet under iOS 6 the scrollViewShouldScrollToTop never gets called. Is this a bug?? The API says this should still be available as part of the delegate in iOS6, yet under iOS6 on both device and simulator it never executes... Anyone have any idea what is going on?
Still no other TableView or ScrollView, but there is a MAPVIEW?? But the MapView doesn't have a shouldScrollToTop that I can find to set to NO.. so I am still beyond confused why this stopped working under iOS 6...
Is there any chance that the UIScrollView you're creating isn't somehow the only UIScrollView in your view hierarchy? It looks like in iOS6, if you have more than a single UIScrollView in your view hierarchy, only one should have scrollsToTop = YES. This is the one that'll have its scrollViewShouldScrollToTop method called.
My problem was similar in that I had a very basic UITableView that would no longer autoscroll to the top when the status bar was tapped. I finally remembered that one of the cells in my tableView uses a UIWebView, and that the cell's webView.scrollView was (correctly, now in iOS6) hijacking the call to scrollViewShouldScrollToTop that, before iOS6, was being made on my tableView.
After setting the tableViewCell's "scrollsToTop = NO", the status bar autoscroll once again worked as it did before. Here's more-or-less how the code looks:
myCustomCellWithAWebView.webView.scrollView.scrollsToTop = NO;
Hope this helps!
On iOS 6, only tap the part above scrollview of status bar can fire scrollsToTop event.
And, that scrollView can't be hidden or 0 alpha.
But it can be covered. or clear background color.
So on iOS 6, you need
dummyScrollView.frame = self.view.bounds;
dummyScrollView.backgroundColor = [UIColor clearColor];

How to navigate b/w three views using IBAction button method and round rectangle button in Xcode?

Opening Xcode 4.2, I chose a empty application template. All im trying to do is make three views, which can be navigated through the help of one button inserted [Round Rectangle Button].
I have made 3 Views with three buttons within them which are
1)Next
2)Next
3)Root
I am relatively new to Xcode. I have imported all the Views into the app delegates. I know i have done everything right. But I think my code for button is wrong.
I inserted a round rectangular button through the object library.
I went in FirstViewController.h, put in -(IBAction)nextbuttonclicked;
I then go in FirstViewController.m, go down, -(IBAction)nextbuttonclicked {
SecondViewController *SVC = [[SecondViewController alloc] init];
[self.window removeFromSuperview];
AppDelegate *appD = (AppDelegate *) [[UIApplication sharedApplication] delegate];
[appD.window addSubview:SVC.view];
}
The error i get is: Thread 1: Program received signal: "EXC_BAD_ACCESS" while
"return UIApplicationMain (argc, argv, nil) NSStringFromClass ([[AppDelegate class]));"
is highlighted hence the thread is coming from here
Someone please help?
Study navigation controller - http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
and use navigation controller's Push and Pop properties for navigations.

Hide/Unhide UINavigationbar when the screen is tapped

I'm very new with iOS Development and I have just created one of my first apps, in my .xib file I have a UINavigationBar that I want to hide/show when a part of the screen is tapped by the user (like in the Photo app). I've found some snippets online but I don't know where and how to use those.
I'd appreciate a lot if somebody could give me detailed informations about how to do this.
Add this toggle method anywhere in your UIViewController. This hides on first tap and shows again in second tap.
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navigationController.navigationBar.hidden;
[self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
}
If there is no navigation controller, link the navigation bar with an IBOutlet and replace with
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navBar.hidden;
self.navBar.hidden = !barsHidden;
}
Then add the following in the method -(void)viewDidLoad {}
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleNavBar:)];
[self.view addGestureRecognizer:gesture];
[gesture release];
If the view where you are going to tap is a UIWebViewController, you have to add the protocol to the view controller and set it as delegate gesture.delegate = self; then add the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This is needed because the UIWebViewController already implements its own gesture recognizers.
Ultimately, you want to send the -setHidden: message to your navigation bar. The easiest way to do this is to make an Outlet and an Action in your in your view controller. Then, in your .xib file, connect the navigation bar to the outlet and some button (even a large, full screen one) to the action.
Outlets and Actions are basic techniques used over and over in iOS
(and Mac) programming, so if you don't understand them, best go read
up on them now. Every beginning iOS/Mac programming book covers this
topic as does Apple's own Getting Started guide (pay particular
attention to the Configuring the View section).
Inside your action, send a message to the outlet like so:
-(void)myButtonAction:(id)sender{
[[self myNavigationBarOutlet] setHidden:YES];
}
This will hide the navigation bar whenever your button is tapped.
(This assumes you have a UINavigationBar in your .xib like you say. These directions will be different if you're working with a UINavigationController that manages its own UINavigationBar)

ios4 - Load new view on clicking a button

I am currently using XCode 3.2.3 and iOS4. I'm working on an app that simply starts with one screen and on a button click moves to the next.
I have gone thorugh the ViewController programming guide and a post here.
What I am doing is very similar to whats happening on the post. So let me explain the steps, I followed:
In IB, drag and drop, a View from the library into the editor. I renamed the new UIView to myView.
In my AppControllerDelegate, I added the new view "myView" as a property of the view controller (File's Owner). I synthesized it as well in the implementation.
Now, in the implementation of the ViewController, within the button pressed action handler, I wrote the following lines of code:
[self.view addSubView: myView];
On clicking the button however, I do not see a new screen or my new view. However if I do this, I get a new screen or new view:
UIView *anotherView = [[UIView alloc] initWithFrame:self.view.frame];
[self.view addSubView: anotherView];
I do know that the best way is to do it with separate NIBs for each UIView. However, I am new to iPhone development and have not explored that path as yet.
My question: What am I missing upto step 3?
One way you could be able to do it is by trying it this way
myView = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle: [NSBundle mainBundle]];
[self.view addSubview: myView.view];
Try that and see if it is working.. if yours is a view based project then instead of [self.view addSubview: myView.view], just give [self.view addSubview : myView];

Resources