1

我有这样的场景:我是 Xcode 的新手,也做过示例。实际上,我使用了 SplitView 控制器,因为它一切正常,但是当谈到详细视图控制器时,我的意思是在带有两个 ViewController 的 Navigation Cotroller 中。

因为我只是使用“选择器”来获取位置,当我按下 get location 时,它会转到第二个视图控制器,但在这里我没有检索到 Vale。这是一段代码

//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 控制器的新手。那么我做错了什么,还是需要在 "delegates """ 处声明任何内容?请做有需要的

4

2 回答 2

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];
于 2013-04-01T05:58:45.587 回答
0

如果您正在从一个控制器到下一个控制器进行转场,则传递值就像在 ViewController1 实现中添加以下代码一样简单:

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

而我自己,我尝试将填充变量最小化到应用程序委托中,这实际上应该主要用于 UIApplicationDelegate 类型的东西,比如应用程序被暂停等。

于 2013-04-01T05:59:37.213 回答