2011-01-09 83 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. 閱讀上的「依賴注入」的設計模式。

希望有所幫助。

相關問題