Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 通过导航控制器传递值_Iphone_Variables_Uiview_Uinavigationcontroller - Fatal编程技术网

Iphone 通过导航控制器传递值

Iphone 通过导航控制器传递值,iphone,variables,uiview,uinavigationcontroller,Iphone,Variables,Uiview,Uinavigationcontroller,我希望将用户选择的值从一个视图传递到下一个视图,以便将其提交到Twitter、Facebook等 全局变量是否最好实现?我不希望将该值存储或保存在任何位置。。只需通过导航控制器(提交到Facebook、Twitter等)的末尾即可 有什么建议吗?提前谢谢 头文件 #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> #import "ShareViewController.h" #include "Twitte

我希望将用户选择的值从一个视图传递到下一个视图,以便将其提交到Twitter、Facebook等

全局变量是否最好实现?我不希望将该值存储或保存在任何位置。。只需通过导航控制器(提交到Facebook、Twitter等)的末尾即可

有什么建议吗?提前谢谢

头文件

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ShareViewController.h"
#include "TwitterRushViewController.h"

@interface KindViewController : UIViewController <UIPickerViewDelegate, UIScrollViewDelegate, CLLocationManagerDelegate> {
            IBOutlet UIScrollView *scrollView;
            IBOutlet UIPageControl *pageControl;
            BOOL pageControlIsChangingPage;

            CLLocationManager *locationManager;
            NSString *finalCoordinates;
            NSString *finalChoice;
            }

@property (nonatomic, retain) UIView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;

@property (nonatomic, retain) CLLocationManager *locationManager; 
@property (retain) NSString *finalCoordinates;
@property (nonatomic, copy) NSString *finalChoice;


-(IBAction)changePage:(id)sender;
-(IBAction)submitChoice:(id)sender;
-(void)setupPage;

@end
#import "KindViewController.h"
#import "JSON/JSON.h"

@implementation KindViewController
@synthesize scrollView;
@synthesize pageControl;
@synthesize locationManager;
@synthesize finalCoordinates;
@synthesize finalChoice;

#pragma mark -
#pragma mark UIView boilerplate
- (void)viewDidLoad {   
    [self setupPage];   
    [super viewDidLoad];

    // Alert the User on Location Access
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    self.locationManager.delegate = self;
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    }

-(void)viewWillAppear:(BOOL)animated {
    [locationManager startUpdatingLocation];    
    }

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocation *location = newLocation;
    NSLog(@"Our current Latitude is %f", location.coordinate.latitude);
    NSLog(@"Our current Longitude is %f", location.coordinate.longitude);
    NSString *Coordinates = [[NSString alloc] initWithFormat: @"Longitude=%f&Latitude=%f", location.coordinate.longitude, location.coordinate.latitude ];
    NSLog(@"Test: %@", Coordinates);
    finalCoordinates = Coordinates;
    [locationManager stopUpdatingLocation]; 
    }

#pragma mark -
#pragma mark The Guts
- (void)setupPage {
    scrollView.delegate = self;
    [scrollView setCanCancelContentTouches:NO];
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView.clipsToBounds = YES;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;

    NSUInteger nimages = 0;
    CGFloat cx = 0;
    for (; ; nimages++) {
        NSString *imageName = [NSString stringWithFormat:@"choice%d.png", (nimages + 1)];
        UIImage *image = [UIImage imageNamed:imageName];
        if (image == nil) {
            break;
        }
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        CGRect rect = imageView.frame;
        rect.size.height = image.size.height;
        rect.size.width = image.size.width;
        rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
        rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);

        imageView.frame = rect;

        [scrollView addSubview:imageView];
        [imageView release];

        cx += scrollView.frame.size.width;
    }
    self.pageControl.numberOfPages = nimages;
    [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
    }


#pragma mark -
#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView {
    if (pageControlIsChangingPage) {
        return;
    }
    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
    }

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
    pageControlIsChangingPage = NO;
    }


#pragma mark -
#pragma mark PageControl stuff
- (IBAction)changePage:(id)sender {
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    pageControlIsChangingPage = YES;
    }


-(IBAction)submitChoice:(id)sender; {

    // Spinner
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(135,140,50,50)];
    [spinner startAnimating];
    [self.view addSubview:spinner];

    // Find the Date
    NSDateFormatter *format = [[NSDateFormatter alloc] init];
    [format setDateFormat:@"MMM dd, yyyy HH:mm"];

    NSDate *now = [[NSDate alloc] init];
    NSString *dateString = [format stringFromDate:now];

    // Echo Everything
    NSLog(@"Type is %f.", scrollView.contentOffset.x);
    NSLog(@"Date is %@", dateString);
    NSLog(@"Coordinates are %@", finalCoordinates);

    NSString *completeURL = [[NSString alloc] initWithFormat: @"http://www.example.com/insert.php?Type=%f&Time=%@&%@", scrollView.contentOffset.x, dateString, finalCoordinates];
    NSString *escapedUrl = [completeURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"URL is %@.", escapedUrl);

    // Post to Web Server
    NSURL *urlToSend = [[NSURL alloc] initWithString:escapedUrl];
    NSLog(@"NSURL is %@.", urlToSend);

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlToSend cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];

    NSData *urlData;
    NSURLResponse *response;
    NSError *error;
    urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];

    // Do the Button Action
    ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:@"ShareViewController" bundle:nil];
    shareViewController.finalChoice = @"Facebook Property";
    [self.navigationController pushViewController:shareViewController animated:YES];
    [shareViewController release];

    [urlToSend release];
    [completeURL release];
    [spinner release];
    }

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"There is an error updating the location");
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    }

- (void)viewDidUnload {
    [super viewDidUnload];
    [pageControl release];
    }

- (void)dealloc {
    [super dealloc];
    }

@end
ShareViewController.h

#import <UIKit/UIKit.h>
#import "MapViewController.h"
#import "SA_OAuthTwitterController.h" 
#import "FBConnect/FBConnect.h"
#import "TwitterRushViewController.h"
#import "oAuth2TestViewController.h"

@class SA_OAuthTwitterEngine;

@interface ShareViewController : UIViewController <UITextFieldDelegate, SA_OAuthTwitterControllerDelegate> {
    }

@property (nonatomic, retain) IBOutlet UIButton *shareFacebookBTN;
@property (nonatomic, retain) IBOutlet UIButton *shareTwitterBTN;
@property (nonatomic, retain) KindViewController finalChoice;


/* Submissions */
- (IBAction)shareNoThanks:(id)sender;
- (IBAction)shareFacebook:(id)sender;
- (IBAction)shareTwitter:(id)sender;

@end
#导入
#导入“MapViewController.h”
#导入“SA_OAuthTwitterController.h”
#导入“FBConnect/FBConnect.h”
#导入“TwitterRushViewController.h”
#导入“oAuth2TestViewController.h”
@SA_类OAuthTwitterEngine;
@接口ShareViewController:UIViewController{
}
@属性(非原子,保留)IBOUTLE UIButton*shareFacebookBTN;
@属性(非原子,保留)IBMOutlet UIButton*shareTwitterBTN;
@财产(非原子,保留)最终选择;
/*意见书*/
-(iAction)共享银行:(id)发送方;
-(iAction)shareFacebook:(id)发件人;
-(iAction)shareTwitter:(id)发件人;
@结束

推送下一个UIViewController时,只需在其上设置属性可能是最简单的操作

SomeViewController *someViewController = [[SomeViewController alloc] init];
someViewController.facebookProperty = @"Facebook Property";
someViewController.twitterProperty = @"Twitter Property";
[self.navigationController pushViewController:someViewController animated:YES];
[someViewController release];
好的,ShareViewController.h有一些问题。首先,如果您有一个属性,您还需要为该属性设置一个实例变量。接下来,您将为finalChoice分配一个字符串,但您已经将其类型声明为KindViewController,它应该是NSString

在ShareViewController.h中

@interface ShareViewController : UIViewController <UITextFieldDelegate, SA_OAuthTwitterControllerDelegate> {
    NSString *finalChoice;
}

@property (nonatomic, retain) IBOutlet UIButton *shareFacebookBTN;
@property (nonatomic, retain) IBOutlet UIButton *shareTwitterBTN;
@property (nonatomic, copy) NSString *finalChoice;
@接口ShareViewController:UIViewController{
NSString*最终选择;
}
@属性(非原子,保留)IBOUTLE UIButton*shareFacebookBTN;
@属性(非原子,保留)IBMOutlet UIButton*shareTwitterBTN;
@属性(非原子,副本)NSString*最终选择;

并确保在实现文件中@synthesis finalChoice。

在不知道任何细节的情况下,我会避免使用全局变量或将两个视图紧密耦合在一起


根据您的需要和实现,或者在视图之间传递变量可能是更好的选择

关于如何最好地在视图控制器之间通信这一更普遍的问题,有一个很好的讨论。得票最多的答案相当不错

tl;dr版本基本上是:

  • 全局变量和单例类很少是正确的答案
  • 阅读“依赖注入”设计模式

  • 希望能有所帮助。

    谢谢亚伦!在下一个视图中,我如何称呼它?我是否使用@synthesis?是的,它们应该定义为SomeViewController中的属性,例如,有一个名为NSString*facebookProperty的实例变量和一个名为@property(非原子,复制)的属性NSString*facebookProperty。然后你应该在实现文件中综合它。。。谢谢那正是我档案里的东西。。但我在非结构或联合中收到了以下错误“请求成员”facebookProperty。“我编辑了我的答案以包含我的代码。嗯,我不确定,那里发生了一些奇怪的事情。”。我会建议你简化你所拥有的,看看你是否能让它发挥作用。如果你仍然没有运气,那么可能会发布更多的代码。我仍在试图找出它。。。但是我已经更新了我的答案,包括了我的整个实现文件。谢谢你!我会仔细看看的。谢谢马特!我感谢你的迅速反应。