Next and previous button like mail application in iPhone - iphone

How to create next and previous button in navigation bar like in mail application in iphone.

Use the next code (notice that you need the "prev.png" and "next.png" images - arrows):
- (void)addNextPrevSegmentedControl {
// Prepare an array of segmented control items as images
NSArray *nextPrevItems = [NSArray arrayWithObjects:[UIImage imageNamed:#"prev.png"], [UIImage imageNamed:#"next.png"], nil];
// Create the segmented control with the array from above
UISegmentedControl* nextPrevSegmentedControl = [[UISegmentedControl alloc] initWithItems:nextPrevItems];
[nextPrevSegmentedControl addTarget:self action:#selector(nextPrevAction:) forControlEvents:UIControlEventValueChanged];
// Create the bar button item with the segmented control from above
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:nextPrevSegmentedControl];
// Add the bar button item from above to the navigation item
[self.navigationItem setRightBarButtonItem:rightButton animated:YES];
// Release memory
[rightButton release];
[nextPrevSegmentedControl release];
}
- (void)nextPrevAction:(id)sender {
if ([sender isKindOfClass:[UISegmentedControl class]]) {
int action = [(UISegmentedControl *)sender selectedSegmentIndex];
switch (action) {
case 0:
// Prev
break;
case 1:
// Next
break;
}
}
}
EDIT: corrected the code

It can be implemented by using a UISegmentedControl with 2 Segments.
Set the segmentedControlStyle as UISegmentedControlStyleBar.
Set 2 UIImage for up and down look.

Related

UIButton is resized without visible cause?

I have ViewController(1) which hierarchy is like this
ViewController
- UIView
-- UITableView (height 80% of it's superview)
-- UIButton ( height 20% of it's superview, 80% offset y)
After pressing the UIButton new ViewController is pushed. Till now everything is okay, but in the moment of the animation I see that UIButton is resized to fit in the entire view. After going back now i have my button on the entire view.
What can cause this problem to appear?
1.
UIPopOverController
- UINavigationController
-- UIViewController
EDIT 1:
- (void)viewDidAppear:(BOOL)animated
{
// ...
_storeButton = [[UIButton alloc] initWithFrame:frame];
_storeButton.adjustsImageWhenHighlighted = NO;
_storeButton.backgroundColor = [UIColor colorWithHexString:(_productData.count > 0) ? #"0x8BC53F" : #"0xA4A4A4" ];
[_storeButton addTarget:self action:#selector(storeButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
// ...
}
#pragma mark - Store request.
- (void)storeButtonTouched:(id)sender
{
DLog(#"_storeButton: %#", _storeButton);
if ( _productData != nil && _productData.count > 0) {
_productData = [NSArray arrayWithArray:_iapManager.productData];
TTIAPTableViewController *iapVC = [[TTIAPTableViewController alloc] initWithStyle:UITableViewStylePlain];
iapVC.entries = _productData;
iapVC.contentSizeForViewInPopover = self.view.frame.size;
[self.navigationController pushViewController:iapVC animated:YES];
[iapVC release];
} else {
DLog(#"No products available.");
}
}
Solved through using Containing ViewControllers.

UISegmentedControl click event not working in IOS 6

I have created a mapview, with page curl feature in it. The mapview is having a toolbar, with a page curl button. On clicking the button, the mapview page curls. Here is the code.
-(IBAction) onPageCurl:(id)sender{
pageCurlViewController = [[MyMapViewPageCurlViewController alloc] initWithNibName:#"MyMapViewPageCurlViewController" bundle:nil];
[pageCurlViewController.navigationController.toolbar setHidden:NO];
[pageCurlViewController setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[pageCurlViewController setToolbarItems:toolbarItems];
[[self navigationController] presentModalViewController:pageCurlViewController animated:YES];
[pageCurlViewController getMapView:&mapView];
[pageCurlViewController release];
}
As the mapview page curls, I have a new viewcontroller underneath it. The new view controller has a segmented control with 3 segments.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.navigationController.toolbar setHidden:NO];
[directionSearchSegmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
directionSearchSegmentedControl.selectedSegmentIndex = selectedIndex;
UIBarButtonItem *directionSearchSegmentedControlButton = [[[UIBarButtonItem alloc] initWithCustomView:directionSearchSegmentedControl] autorelease];
NSArray *toolbarItems = [NSArray arrayWithObjects: navigatorButton , flexibleSpace, directionSearchSegmentedControlButton, flexibleSpace, pageCurlButton, nil];
[self setToolbarItems:toolbarItems];
[self.navigationController.toolbar setHidden:NO];
}
I have standard/satellite/hybrid view of the map on clicking each segments in the segmented controller.
- (void)segmentAction:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
if([sender selectedSegmentIndex] == 0){
selectedIndex = 0;
pageCurlMapView.mapType = MKMapTypeStandard;
}
if([sender selectedSegmentIndex] == 1){
selectedIndex = 1;
pageCurlMapView.mapType = MKMapTypeSatellite;
}
if([sender selectedSegmentIndex] == 2){
selectedIndex = 2;
pageCurlMapView.mapType = MKMapTypeHybrid;
}
if([sender selectedSegmentIndex] == 2){
}
directionSearchSegmentedControl.momentary = YES;
selectedIndex = directionSearchSegmentedControl.selectedSegmentIndex;
}
The page curl feature is working fine. As the page curls, as mentioned before, I have a segmented control in the new view. But the the segmented control is not working properly in IOS 6. I have debugged and checked. On the click of the segments, the control doesn't enter the event method.
Its still working fine in the previous versions of IOS, but not in IOS 6. Cant figure out, what is wrong. Help needed.
If you look at the Xcode console when the app launches, do you see a message like this:
Applications are expected to have a root view controller at the end of application launch
If so, then your you problem could be due to changes in how the root view controller needs to be set up for iOS 6.
See here for info on how to address the problem.
Sorry if this is kinda late, but I have been working on something similar in my app. Honestly, I just used a simple switch method in order to change the map type. The code is below.
- (IBAction)toggle:(id)sender {
switch ([sender selectedSegmentIndex]) {
case 0:
{
[self.mapView setMapType:MKMapTypeStandard];
}break;
case 1:
{
[self.mapView setMapType:MKMapTypeHybrid];
}break;
case 2:
{
[self.mapView setMapType:MKMapTypeSatellite];
}break;
}
}
Now I have not tried using this with multiple view controllers so this may only work with the one view controller. I just started programming on iOS so bear with me.. I'm gonna try to add the page curl effect to my app but all I have so far is a map view and a button that when it's pushed, zooms into the users location and the user is able to change the map type..
P.S. This is a Segmented Control set as a Value Changed action named "toggle" if that helps...

Activity Indicator not starting animation straight away

I initialize an activity indicator and in a button press action I start it animating and call the next view to display.
-(IBAction) downloadButtonPressed:(id)sender {
NSLog(#"Download Button Pressed");
indicator.hidden = NO;
[indicator startAnimating];
if (addviewcontroller == nil)
addviewcontroller = [[AddViewController alloc]init];
[self.view addSubview:addviewcontroller.view];
[addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:addviewcontroller animated:YES];
}
When I press the button, the activity indicator doesn't start immediately. It starts when the other view is called. The indicator is displayed for a second, but when the button is pressed it takes some time to load the other view.
I dont know why the indicator shows for a second without starting.
Try this :
-(IBAction) downloadButtonPressed:(id)sender
{
NSLog(#"Download Button Pressed");
indicator.hidden = NO;
[indicator startAnimating];
[self performSelector:#selector(showController) withObject:nil afterDelay:0.1f];
}
- (void)showController {
if (addviewcontroller == nil)
addviewcontroller = [[AddViewController alloc]init];
[self.view addSubview:addviewcontroller.view];
[addviewcontroller setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:addviewcontroller animated:YES];
}
That should do the trick ;-)
EDIT
I just noticed that there is a problem in your code, you are adding your addviewcontroller twice. One by adding it as a subview of the actual view controller, and one by modally presenting another view controller. You should remove one of the statements from this function.

How to convert navigationcontroller's back button into browser's back button in iPhone?

I am showing a some text and images in UIwebview in navigation based application. It also contains some link to the source of the data i.e the websites. It is showing it in perfect manner. But when user click on the back button it pop the previous viewcontroller and goes to it. But, What I want when user click on the link the back button must be converted into browser's back button and act like it.
Any suggestions/sample code or tutorial for it?
You can replace the navigation bar's back button with a browser back button whenever the UIWebView has the option to go back by doing something like this:
- (void)updateBackButton {
if ([self.webView canGoBack]) {
if (!self.navigationItem.leftBarButtonItem) {
[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)] autorelease];
[self.navigationItem setLeftBarButtonItem:backItem animated:YES];
}
}
else {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.navigationItem setHidesBackButton:NO animated:YES];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
See First thing that you have to do is, that you have to hide the default back button, as follows below
self.navigationItem.hidesBackButton = YES;
After this you have to add a custom back button on the navigationbar as below:
UIButton *m_BackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
m_BackBtn.frame = CGRectMake(4.0, 5.0+0.0, 100, 30);
[m_BackBtn setTitle:#"Back" forState:UIControlStateNormal];
[m_BackBtn addTarget:selfaction:#selector(BackButtonAction)forControlEvents:UIControlEventTouchUpInside];
[m_BackBtn retain];
[self.navigationController.navigationBar addSubview:m_BackBtn];
*And in the BackButtonAction Function *
-(void)BackButtonAction
{
[yourwebView goBack];
// Do your stuff as you required
}
Take a UIBarbuttonItem from XIB at the place of back button and you
can write following in its IBAction method Suppose your UIWebview
object is
UIWebView *web;
-(IBAction)goBack
{
[web goBack];
}
goBack is inbuilt method of UIWebView
I like #cduhn's approach above for it's simplicity, but it has the downside of losing the "<" on the Back button. See this answer if you want to keep the "real" back button (with it's "<" icon), but change the button's behavior:
https://stackoverflow.com/a/19132881/132542
For the web view history case here, this is the code I used in my delegate method:
-(BOOL) navigationShouldPopOnBackButton {
if (webView.canGoBack) {
// Then just go back in the web view
[webView goBack];
// return NO to keep the current view controller loaded
return NO;
}
else {
// no web history, so pop the view
return YES;
}
}
This is a bit tricky solution, may not be suggested. But this way you can maintain the back navigation animation on navigation bar.
var urlRequest: NSURLRequest?
////////
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == .LinkClicked {
//AViewController creates and pushes itself to the navigationController
let pushViewController = self.storyboard?.instantiateViewControllerWithIdentifier("AViewController") as! AViewController
pushViewController.urlRequest = request
self.navigationController?.pushViewController(pushViewController, animated: true)
return false
} else { //is initial request from push
return true
}
}

How to navigate the view in iphone programming?

In my appController's ViewDidLoad, I have done some thing as below
- (void)viewDidLoad
{
self.overlayViewController =
[[[OverlayViewController alloc] initWithNibName:#"OverlayViewController" bundle:nil] autorelease];
// as a delegate we will be notified when pictures are taken and when to dismiss the image picker
self.overlayViewController.delegate = self;
self.capturedImages = [NSMutableArray array];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// camera is not on this device, don't show the camera button
NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:self.myToolbar.items.count];
[toolbarItems addObjectsFromArray:self.myToolbar.items];
[toolbarItems removeObjectAtIndex:2];
[self.myToolbar setItems:toolbarItems animated:NO];
}
}
I have two methods as below,
- (IBAction)cameraAction:(id)sender
{
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if (self.imageView.isAnimating)
self.imageView.stopAnimating;
if (self.capturedImages.count > 0)
[self.capturedImages removeAllObjects];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.overlayViewController setupImagePicker:sourceType];
[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
}
}
Now as I click the button, the method will launch the class showing custom view, I want to call this without clicking the button, what should I do ?
I wrote the button coding directly in ViewDidLoad, but not working at all
This code, I took from apple's documentation as example
Help !
If I understand correctly, you are wanting to show a view?
If so you could push using:
[self.navigationcontroller pushviewcontroller:YOURVIEWCONTROLLER animated:YES];
Or you could present it using:
[self presentModalViewControllerpushviewcontroller:YOURVIEWCONTROLLER animated:YES];