![](/img/trans.png)
[英]react-native-push-notification doesn't work on ios DEVICE
[英]iOS React Native - navigation doesn't work using native code
我正在嘗試使多個框架在iOS環境中協同工作,而在嘗試使用由React Native(RN)觸發的本機方法進行導航時,我陷入了困境。
嘗試了多種方法(其中之一是公開rootViewController
並在觸發的方法中更改UIViewController
)之后,即使調用了該方法,我也無法更改視圖控制器。
我將在下面留下一些我嘗試過的代碼,並希望能更好地解釋我要做什么。
免責聲明:我是obj-c的初學者,並且是iOS開發的完整菜鳥-我在學習
這是我的AppDelegate
實現:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[super applicationDidFinishLaunching:application];
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"SomethingMobile"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
UIViewController *rootViewController = [[UIViewController alloc] init];
rootViewController.view = rootView;
self.navigationController = [[UINavigationController alloc] initWithRootViewController: rootViewController];
[self.window setRootViewController:self.navigationController];
//--- style the UINavigationController
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
self.navigationController.navigationBar.topItem.title = @"Home";
return YES;
}
-(void) changeView {
// SimpleViewController is a valid UIViewController that I tested separately and it works
[self.navigationController pushViewController:[[SimpleViewController alloc] init] animated:YES];
self.navigationController.navigationBar.topItem.title = @"OF";
printf("Got here");
}
@end
然后,我有一個充當RN與本機代碼之間橋梁的類:
@implementation OFStarter
// The React Native bridge needs to know our module
RCT_EXPORT_MODULE()
- (NSDictionary *)constantsToExport {
return @{@"greeting": @"Welcome to native"};
}
RCT_EXPORT_METHOD(squareMe:(int)number:(RCTResponseSenderBlock)callback) {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate changeView]; // this is getting called when I press a button in the RN view
callback(@[[NSNull null], [NSNumber numberWithInt:(number*number)]]);
}
@end
現在由於某種未知的原因,當調用changeView
方法時,什么也沒有發生。 據我了解, pushViewController
是使用UINavigationController
更改視圖時要調用的方法。
現在,如果我想使用與changeView
相同的代碼來更改didFinishLaunchingWithOptions
方法中的視圖控制器,則它可以完美地工作。
[self.navigationController pushViewController:[[SimpleViewController alloc] init] animated:YES];
更奇怪的是,有一次我單擊RN按鈕時煩惱了約20次,然后突然視圖開始改變了約20次。
另外,我想提到的是,當我按下RN按鈕時, changeView
總是被調用。
對此,我將不勝感激。 我在這個問題上停留了一段時間,我很確定自己可能會遺漏一些明顯的東西。 謝謝!
我最終弄清楚了問題。 更改ViewController時,必須從主線程執行此操作,否則它將無法按預期工作。
我要做的就是將此方法添加到OFStarter
實現中:
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.