从一个tabBar跳转到另一个tabBar页面,但是无法点击跳转到第一个tabBar页面
时间: 2023-11-29 22:07:05 浏览: 53
这个问题可能是因为你在第一个tabBar的页面上调用了`tabBarController?.selectedIndex`来跳转到第二个tabBar的页面,但是这个方法只会改变tabBar的选中项,并不会触发tabBar的点击事件。因此,当你想要从第二个tabBar跳转回第一个tabBar时,第一个tabBar的页面并没有被点击,导致无法响应跳转事件。
解决方法是,在第一个tabBar的页面上,不要直接调用`tabBarController?.selectedIndex`,而是使用`tabBarController?.selectedViewController`来获取当前选中的视图控制器,然后再在该视图控制器中调用跳转方法。这样就能保证页面能够正常响应跳转事件了。
相关问题
从一个tabBar跳转到另一个tabBar页面,但是无法点击跳转到第一个tabBar页面,具体代码实现
假设你要从第一个 tabBar 页面跳转到第二个 tabBar 页面,然后在第二个 tabBar 页面中,你需要能够点击跳转回第一个 tabBar 页面。这里给出一个简单的实现方式:
在第一个 tabBar 页面中,添加一个按钮,当用户点击该按钮时,触发跳转到第二个 tabBar 页面的操作,具体代码如下:
```
// 在第一个 tabBar 页面的某个 ViewController 中添加一个按钮
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("跳转到第二个tabBar", for: .normal)
button.addTarget(self, action: #selector(goToSecondTabBar), for: .touchUpInside)
view.addSubview(button)
// 实现跳转到第二个 tabBar 页面的方法
@objc func goToSecondTabBar() {
if let tabBarController = self.tabBarController {
tabBarController.selectedIndex = 1 // 切换到第二个 tabBar 页面
}
}
```
在第二个 tabBar 页面中,添加一个按钮,当用户点击该按钮时,触发跳转回第一个 tabBar 页面的操作,具体代码如下:
```
// 在第二个 tabBar 页面的某个 ViewController 中添加一个按钮
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
button.setTitle("跳转回第一个tabBar", for: .normal)
button.addTarget(self, action: #selector(goToFirstTabBar), for: .touchUpInside)
view.addSubview(button)
// 实现跳转回第一个 tabBar 页面的方法
@objc func goToFirstTabBar() {
if let tabBarController = self.tabBarController {
tabBarController.selectedIndex = 0 // 切换回第一个 tabBar 页面
}
}
```
这样就可以在第一个 tabBar 页面和第二个 tabBar 页面之间来回跳转了,并且可以在第二个 tabBar 页面中点击按钮跳转回第一个 tabBar 页面。
flutter怎么通过路由从别的页面跳转到TabBar和TabBarView里面页面的某一个不特定的页面
要从另一个页面跳转到TabBar和TabBarView中的某个特定页面,您可以在页面之间传递参数,并在TabBarView中使用参数来确定当前应该显示哪个页面。以下是一个示例:
假设您有三个页面:HomePage、TabbedPage和DetailPage。HomePage包含一个按钮,点击该按钮将导航到TabbedPage,并传递一个标识参数用于确定应该显示哪个页面。TabbedPage包含TabBar和TabBarView,每个标签对应一个页面。DetailPage是TabBarView中的某个特定页面。
首先,在HomePage中,您可以使用Navigator.push方法跳转到TabbedPage,并传递一个参数来标识应该显示哪个页面:
```dart
import 'package:flutter/material.dart';
import 'tabbed_page.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: Center(
child: RaisedButton(
child: Text('Go to Tabbed Page'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => TabbedPage(selectedIndex: 1)), // 传递参数selectedIndex为1
);
},
),
),
);
}
}
void main() {
runApp(MaterialApp(
home: HomePage(),
));
}
```
然后,在TabbedPage中,您可以通过DefaultTabController和TabBarView来管理标签和相应的页面。通过接收传递的selectedIndex参数来确定初始选中的页面。
```dart
import 'package:flutter/material.dart';
import 'tab_page.dart';
class TabbedPage extends StatelessWidget {
final int selectedIndex;
TabbedPage({this.selectedIndex});
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 3,
initialIndex: selectedIndex,
child: Scaffold(
appBar: AppBar(
title: Text('Tabbed Page'),
bottom: TabBar(
tabs: [
Tab(text: 'Page 1'),
Tab(text: 'Page 2'),
Tab(text: 'Page 3'),
],
),
),
body: TabBarView(
children: [
TabPage(title: 'Page 1'),
TabPage(title: 'Page 2'),
DetailPage(),
],
),
),
);
}
}
class TabPage extends StatelessWidget {
final String title;
TabPage({this.title});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(title),
),
);
}
}
class DetailPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text('Detail Page'),
),
);
}
}
```
在上面的示例中,我们在TabbedPage中接收selectedIndex参数,并使用initialIndex来确定初始选中的页面。在TabBarView中,我们根据传递的selectedIndex来决定显示哪个页面。
当您从HomePage点击按钮跳转到TabbedPage时,将会显示TabBar和TabBarView,并且特定的页面(例如DetailPage)将会被选中。
希望对您有所帮助!如果有任何其他问题,请随时提问。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)