[英]Passing Data between View Controllers : from uitableview to a details view controller
[英]Passing VALUE between 2 View Controllers in SplitView Controller
我有這樣的場景:我是Xcode的新手,並且還完成了示例。 實際上,我使用SplitView Controller的原因是,一切正常,但是當涉及到細節View控制器時,我的意思是在帶有兩個ViewController的Navigation Cotroller中。
因為我只使用了一個“ Picker”來獲取位置,並且當我按下get location時,它會選擇到2nd View Controller,但是在這里我並沒有檢索到谷歌。 這是一段代碼
//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
但是就像要移到另一個Controller一樣。 它只顯示“ null”。 我是SplitView Controller的新手。 那我做錯什么了嗎,還是需要在“代表”處聲明任何東西? 請做有需要的
最好將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];
如果要在一個控制器與另一個控制器之間進行排序,則傳遞值就像在ViewController1實現中添加以下代碼一樣容易:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
QuerySubmission * viewController2 = segue.destinationViewController;
if(viewController2)
{
viewController2.location=[pickerViewArray objectAtIndex:selectedIndex];
}
}
而我自己,我嘗試將填充變量最小化到應用程序委托中,這實際上應該主要用於UIApplicationDelegate類型的事情,例如被暫停的應用程序等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.