根据我的阅读,我了解到我需要在AppDelegate中设置一个引用我的视图控制器的变量,以便我可以做这样的事情。接收条形码扫描,然后调用控制器中的方法并传入条形码扫描。所以我在AppDelegate中有这个。
- (void)BarcodeDataArrived:(char *)BarcodeData;
{
[myViewController LoadBarcodePage:BarcodeData];
}
我知道当我的蓝牙扫描程序扫描条形码时会调用此方法。问题是myViewController对象没有引用当前视图控制器,因此所有对象都为null。
我认为我不想创建myViewController的新实例,因为故事板在应用加载时已经创建了它的实例。我只是希望能够引用故事板创建的同一个对象。所以,如果我正确理解事物,我需要在我的AppDelegate中做这样的事情来设置变量:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// I Want To Set A Reference To My View Controller Here Such As...
myViewController = aViewController;
// Where aViewController Is What The Storyboard Initialized.
return YES;
}
我该怎么做?我在NET上找到的所有引用似乎都涉及笔尖而不是故事板。
这是我的第一个应用。求救!
答案 0 :(得分:0)
您不应在App Delegate中实现该功能。 你为什么不尝试在ViewController中实现你的BarcodeDataArrived
- (void)BarcodeDataArrived:(char *)BarcodeData {
[self performSegueWithIdentifier:@"toMyViewControllerSegue"
sender:self];
}
然后在prepareForSegueMethod中,您可以将数据发送到目标控制器
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
myViewController *destinationController= (myViewController * )segue.destinationViewController;
destinationController.BarcodeData=BarcodeData;
}
现在,如果你不想更改视图,那么你必须尝试另一种方法,myViewController应该是一个对象(或类),并在那里实现方法LoadBarcodePage。您可以在委托中拥有该对象,并且可以使用
在任何类中引用它 AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.myVar doWhatever you want];