我有这样的场景:我是Xcode的新手并且也完成了样本。实际上我使用的是SplitView Controller,一切正常,但是当涉及到细节View控制器时,我的意思是在带有两个ViewControllers的Navigation Cotroller中。
因为我只是使用了一个“Picker”来获取位置,当我按下获取位置时,它就转向第二个视图控制器,但是在这里我没有检索到这个值。这是一段代码
// 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 :(得分: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];
答案 1 :(得分:0)
如果您正在从一个控制器到另一个控制器执行segue,传递值就像在ViewController1实现中添加这些代码一样简单:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
QuerySubmission * viewController2 = segue.destinationViewController;
if(viewController2)
{
viewController2.location=[pickerViewArray objectAtIndex:selectedIndex];
}
}
而我自己,我尝试将填充变量最小化到app委托中,这实际上应该主要用于UIApplicationDelegate类型的东西,比如正在暂停的应用程序等。