UINavigation bar reload/refresh button code - iphone

I know that at the end my question me sound silly and easy to solve but I have been trying for hours to make it work but it does not work at all.
I have a UINavigation bar with a "back" and a "refresh" button. The back button works perfectly but I haven't found any way to make the refresh button to work.
What I want from the refresh button is to load the whole view again. Like refreshing the whole "viewWillAppear" section.
I have tried view reload and setNeedsDisplay but none of the work.
Is there any way that I can do this?
Can anyone please help me?
Thanks a lot

Move all functions called in viewWillAppear: into a seperate function with appropriate name and call them when someone clicks the refresh button and in the viewWillAppear:. Example
-(void) gpsRedirect{
//Do stuff that you did in viewWillAppear:
}
And use this code for creating a selector that's activated for touch up inside event upon a button. This will trigger the method gpsRedirect which contains what you previously did in viewWillAppear:
[barButtonItemName addTarget:self
action:#selector(gpsRedirect)
forControlEvents:UIControlEventTouchUpInside];

Related

Trying to change views after button is pressed

I'm trying to execute code when a button is pressed for an application but I can't find how to change the views after the code is executed. Is there a way to switch views how I want to or is there another way? Thank you in advanced, I'm very new to xcode.
edit: I'm trying to go from one view to another, not the view controller and yes I have one storyboard that I planned on using for the whole project if possible.
To execute code when a button is pressed, you have to set up a method that the button is hooked into. Because you said you're using storyboards, I'll assume your button is on the storyboard. Go to the assistant editor, hold ctrl, and click-and-drag from the button to the view controller's .m file (#implementation section). This will create an IBAction method, and any code in this method will execute whenever you press the button.
The method will look like this:
- (IBAction)aButtonPress:(id)sender {
}
According to your comments, you say you only want to change the on-screen view, and not transition from one view to the next.
Views are added and removed with the following methods:
[aSuperview addSubview:aSubview];
[aSubview removeFromSuperview];
I can't really tell you much else with a lot more detail from you... and even though I asked twice in the comments and you said you only want to change the view, not the view controller... I think you probably need to transition to a new view controller. Impossible to know for sure as you've given almost no detail...
But if you want to transition between view controllers (which also transitions views), then ...
Create two view controllers on the storyboard. Hook them together with a segue, and give that segue a name. Now, to perform that segue in code:
[self performSegueWithIdentifier:#"YOUR_SEGUE_NAME" sender:self];

How to refresh UIViewController view on click of some buttons

I'm developing an iPhone app, i have a UINavigationControlller in my AppDelegate.i have refresh button on other view.Button have an IBAction method on clicking a button. i am using this code
[AppDelegate.navigationController.topViewController.view setNeedsDisplay];
My problem is how can i refresh my page(reload) on click of these button,setNeedsDisplay method not called view life cycle methods eg. viewDidLoad,viewWillAppear
Am I doing it correct or is there a way of doing what I do?
thanks in Advance.
Instead of reloading the viewController, you should write a method (or two, or more) that refreshes the data you need to.
That will avoid collateral problems while you add functions later.
setNeedsDisplay does not call into view controller life cycle methods like viewDid/Will ... - you are calling setNeedsDisplay on a UIView, not on a UIViewController.
setNeedsDisplay triggers a redraw of the UIView and it's subviews;
put a breakpoint into your view's drawRect: and you will see it is hit when pressing the button.

Any way to prevent UIActionSheet from being dismissed when user clicks a button in it?

I added a UIProgressView to my UIActionSheet to indicate the download progress. So I do not want my UIActionSheet to be dismissed when user click the download button in it.
But is there a way to do that ?
Right now I implement didDismissWithButtonIndex delegate method to show UIActionSheet again after it was dismissed. But I was hoping there is a better way.
UIActionSheet is a modal view, which means that it blocks UI completely. Since blocking UI is considered a bad practice, preventing an action sheet from dismissing is not supported. Instead, you should make a completely separate progress indicator outside the action sheet.
#Yar, thanks for the answer, I will try your solution some other time.
But I am also satisfied with my current solution. UIActionSheet.h said
// Called when a button is clicked. **The view will be automatically dismissed after this call returns**
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
So I guess hit any buttons will always dismiss action sheet. Then my solution is first hide progress bar; when user hits download button, show action sheet again but hide the download button and show progress bar.
The cancel button is always there in case user wants to cancel download even after download is started.

Where should I "save" changes in my iPhone view to 'create new' of an object?

I have a view that creates a new core data managed object, and fills in all the required properties and also allows optional ones. I originally had a "Done" button on the top left, and when that was pressed, I validated the object then saved and removed the view.
Now I have an edit/done type setup on the top right, so sometimes there are two identical "Done" buttons on the top of the view. I want to switch the left side button so that it just has the normal "Back" button, then somehow validate and stop the view from being removed if it doesn't validate. I can't find any way to capture the method called by that back button and modify it, and viewWillDisappear doesn't work cause there's no way to abort the disappearing.
How can I make this work? I need to validate this, then save, then remove the view if validate and save worked only.
It sounds like your view is a perfect candidate to be pushed modally instead of through the navigation controller stack.
Push the view that creates your NSManagedObject modally:
[self presentModalViewController:yourViewController animated:YES]
Then continue to use your top right EDIT/DONE button for editing/validation as you currently are and when validation is successful simply save your object and dismiss the modal view controller from the parent view controller:
[[self parentViewController] dismissModalViewControllerAnimated:YES];
For more details check http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14
If you still want to use a button on the left hand side perhaps you can change the right button to say EDIT/CANCEL and add a DONE button on the left side that is only visible when you're not in EDIT mode. If appropriate you can point the DONE button to run through the same validation process before dismissing the modal view using the code above but it probably makes sense that the EDIT/CANCEL button takes care of it.
I hope this helps.
Rog
There is no documented way to intercept the standard back button of UINavigationController. If you want this functionality, your only option would be to customize leftBarButtonItem with a custom button.
When the user taps that button, you can first validate your object and then call popViewControllerAnimated:.
It's hard to mimic the look of the built-in back button, though.

Navigating iPhone Views/ViewControllers

In my iPhone application, I use a Tab Bar Controller with five items for the main navigation. However, I also need additional ways to change the current view. For example, if the user is on the Calendar tab and clicks on a date, they need to be shown a different view.
In order to implement this (and other) types of custom navigation, what do I do? I thought what I needed was to add the Navigation Controller to my Main Window nib, but this looks like it adds a navigation bar to the UI, so I don't think that's it. Basically, what is the best way to change the view when a user clicks on something like a button or item on a grid? I know how to hook these interface items to events, but don't quite get how the logic to change views goes in the Main Window nib.
Thanks!
EDIT: To clarify, I believe what I'm trying to do is navigate to a child view for that tab (not change the active tab). As Griffo commented, yes, I am trying to assemble most of the workings in IB and then tweak the code as necessary. I will be trying the approach from his attached link and reporting back.
I think this SO question answers your question
How about showing modal dialog? For instance if you have UIButton in your tab controller:
associate it with method:
[myButton addTarget:self action:#selector(onDoSomething:) forControlEvents: UIControlEventTouchUpInside];
in method -(void)onDoSomething:(id)_sender open your modal dialog:
[self presentModalViewController:mDoingsomethingController animated:YES];
implement "doing something" controller class and supply it with 'done' button and method 'onDone' containing
[self dismissModalViewControllerAnimated:YES];