2011-01-09 43 views
2

我期待从一个视图传递用户选择的值到下一个,因此它可以被提交到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 

回答

2

Ĵ当你推动它时,在下一个UIViewController上设置一个属性可能是最简单的事情。

好吧,有一些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; 

并确保你在实现文件@synthesize finalChoice。

+0

感谢阿龙!我如何在下一个视图中调用它?我使用@synthesize? – 2011-01-09 23:00:36

+0

是的,他们应该被定义为SomeViewController中的属性,例如有一个名为的NSString * facebookProperty实例变量和属性@property(非原子,副本)的NSString * facebookProperty。然后你应该在实现文件中综合它。 – 2011-01-09 23:03:45

+0

嗨亚伦......谢谢!这正是我在我的文件中..但我得到以下错误“请求成员”facebookProperty“在某些不是结构或联盟。“我编辑了我的答案,包括我的代码 – 2011-01-09 23:28:04

0

不知道任何细节,我会避免一个全局变量或这两种观点紧密耦合在一起。

根据您的需求和实施,delegationnotifications可能是一个更好的选择,以在视图之间传递变量。

0

有一个关于如何最好地视图控制器over here之间沟通的更普遍的问题好商量。最高票数的答案相当不错。

的文艺青年最爱的版本基本上是:

  1. 全局变量和单类很少是正确的答案。
  2. 阅读上的“依赖注入”的设计模式。

希望有所帮助。

相关问题