2013-04-01 1 views
1

다음과 같은 시나리오가 있습니다. Xcode를 처음 사용하고 샘플도 수행했습니다. 실제로 SplitView Controller를 사용하여 모든 것이 잘 작동하지만, View Controller에 대해서는 두 개의 ViewController가있는 Navigation Cotroller에서 사용됩니다.SplitView 컨트롤러에서 두 개의보기 컨트롤러 사이에 VALUE 전달

방금 ​​위치를 가져 오는 데 "피커"를 사용했는데 위치를 가져 오면 두 번째보기 컨트롤러에 연결됩니다.하지만 여기에서는 값을 검색하지 않습니다. 다음 코드 조각은

//ViewController1.h

#import "QuerySubmission.h" 

@interface MDDetailViewController : UIViewController <UISplitViewControllerDelegate> 
{ 
    IBOutlet UIPickerView *pickerView; 
    NSArray *pickerViewArray; 
    NSString *setLocation; 


} 

@property (nonatomic, retain) NSArray *pickerViewArray; 
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView; 
@property(nonatomic,retain) NSString *setLocation; 

@property (strong, nonatomic) id detailItem; 

@property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel; 

- (IBAction)getLocation:(id)sender; 

@end 

//ViewController1.m

다음
- (IBAction)getLocation:(id)sender { 
    int selectedIndex = [self.pickerView selectedRowInComponent:0]; 
    NSString *message = [NSString stringWithFormat:@"Selected Location: %@",[pickerViewArray objectAtIndex:selectedIndex]]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 

    QuerySubmission *obj = [[QuerySubmission alloc] initWithNibName:@"QuerySubmission" bundle:nil]; 
    obj.location=[pickerViewArray objectAtIndex:selectedIndex]; 

    //NSLog(@"loc %@", [ pickerViewArray objectAtIndex:selectedIndex]); 
    //[ pickerViewArray objectAtIndex:selectedIndex]; 
} 

나는 위치 목록을 얻고

를 검색 해요 // ViewController2.h

#import <UIKit/UIKit.h> 
#import "MDDetailViewController.h" 

@interface QuerySubmission : UIViewController <UISplitViewControllerDelegate> 
{ 
    NSString *location; 
} 

@property(nonatomic,retain) NSString *location; 
- (IBAction)querySubmit:(id)sender; 

@end 

다른 컨트롤러로 이동하는 것과 같습니다. 그냥 "null"을 출력합니다. SplitView Controller를 처음 사용했습니다. 그래서 나는 잘못을 저질렀 는가, 아니면 "대의원" ""에서 무엇인가를 선언해야합니까? 필요가 있습니다.

답변

0

당신이 다음에 하나의 컨트롤러에서 SEGUE을하고 있다면, 값을 전달하는 것은 ViewController1 구현에이 코드를 추가하는 것만큼이나 쉽습니다.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    QuerySubmission * viewController2 = segue.destinationViewController; 
    if(viewController2) 
    { 
     viewController2.location=[pickerViewArray objectAtIndex:selectedIndex]; 
    } 
} 

그리고 나 자신이 응용 프로그램이 일시 중지되는 것과 같은 UIApplicationDelegate 유형의 것들에 대해 주로 사용해야하는 응용 프로그램 대리인에 채우기 변수를 최소화하려고 노력합니다.

+0

정말 고맙습니다. –

0

그런 작업에 AppDelegate를 사용하는 것이 좋습니다.

@property(nonatomic,retain) NSString *location;AppDelegate.h 파일에 선언하십시오.

이 같은 getLocation: 방법을 다시 쓰기 :에서

- (IBAction)getLocation:(id)sender 
{ 
    int selectedIndex = [self.pickerView selectedRowInComponent:0]; 
    NSString *message = [NSString stringWithFormat:@"Selected Location: %@",[pickerViewArray objectAtIndex:selectedIndex]]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [[[UIApplication sharedApplication] delegate] setLocation:(NSString *)[pickerViewArray objectAtIndex:selectedIndex]]; 
} 

QuerySubmission 사용 위치에 액세스 할 수 있습니다

[[[UIApplication sharedApplication] delegate] location]; 
+0

감사합니다. –

+0

@KumarKl : 즐거움과 함께 :) –

관련 문제