React Native知識6-NavigatorIOS組件

来源:http://www.cnblogs.com/wujy/archive/2016/09/07/5849769.html
-Advertisement-
Play Games

NavigatorIOS包裝了UIKit的導航功能,可以使用左劃功能來返回到上一界面。本組件並非由Facebook官方開發組維護。這一組件的開發完全由社區主導。如果純js的方案能夠滿足你的需求的話,那麼我們建議你選擇Navigator組件(理論知識可以見React Native中文網)。 一:概念內 ...


NavigatorIOS包裝了UIKit的導航功能,可以使用左劃功能來返回到上一界面。本組件並非由Facebook官方開發組維護。這一組件的開發完全由社區主導。如果純js的方案能夠滿足你的需求的話,那麼我們建議你選擇Navigator組件(理論知識可以見React Native中文網)。

一:概念內容

1:路由:一個路由是用於描述導航器中一頁的對象。NavigatorIOS的第一個路由通過initialRoute屬性來提供。

render: function() {
  return (
    <NavigatorIOS
      initialRoute={{
        component: MyView,
        title: 'My View Title',
        passProps: { myProp: 'foo' },
      }}
    />
  );
},

現在MyView會被導航器渲染出來。它可以通過route屬性獲得對應的路由對象,導航器本身,還有所有passProps中傳遞的屬性。 查看initialRoute的propTypes來瞭解路由(route)的完整定義。

2:導航器:導航器是一個object,包含了一系列導航函數,可供視圖調用。它會作為props傳遞給NavigatorIOS渲染的任何組件。

var MyView = React.createClass({
  _handleBackButtonPress: function() {
    this.props.navigator.pop();
  },
  _handleNextButtonPress: function() {
    this.props.navigator.push(nextRoute);
  },
  ...
});

一個導航器對象包含如下的函數:

push(route) - 導航器跳轉到一個新的路由。
pop() - 回到上一頁。
popN(n) - 回到N頁之前。當N=1的時候,效果和pop()一樣。
replace(route) - 替換當前頁的路由,並立即載入新路由的視圖。
replacePrevious(route) - 替換上一頁的路由/視圖。
replacePreviousAndPop(route) - 替換上一頁的路由/視圖並且立刻切換回上一頁。
resetTo(route) - 替換最頂級的路由並且回到它。
popToRoute(route) - 一直回到某個指定的路由。
popToTop() - 回到最頂層的路由。

導航函數也可以從NavigatorIOS的子組件中獲得:

var MyView = React.createClass({
  _handleNavigationRequest: function() {
    this.refs.nav.push(otherRoute);
  },
  render: () => (
    <NavigatorIOS
      ref="nav"
      initialRoute={...}
    />
  ),
});

二:屬性

1:barTintColor string 

導航條的背景顏色。

2:initialRoute {component: function, title: string, passProps: object, backButtonIcon: Image.propTypes.source, backButtonTitle: string, leftButtonIcon: Image.propTypes.source, leftButtonTitle: string, onLeftButtonPress: function, rightButtonIcon: Image.propTypes.source, rightButtonTitle: string, onRightButtonPress: function, wrapperStyle: [object Object]} 

NavigatorIOS使用"路由"對象來包含要渲染的子視圖、它們的屬性、以及導航條配置。"push"和任何其它的導航函數的參數都是這樣的路由對象。

3:itemWrapperStyle View#style 

導航器中的組件的預設屬性。一個常見的用途是設置所有頁面的背景顏色。

4:navigationBarHidden bool 

一個布爾值,決定導航欄是否隱藏。

5:shadowHidden bool 

一個布爾值,決定是否要隱藏1像素的陰影

6:tintColor string 

導航欄上按鈕的顏色。

7:titleTextColor string 

導航器標題的文字顏色。

8:translucent bool 

一個布爾值,決定是否導航條是半透明的。

9:interactivePopGestureEnabled bool 

決定是否啟用滑動返回手勢。不指定此屬性時,手勢會根據navigationBar的顯隱情況決定是否啟用(顯示時啟用手勢,隱藏時禁用手勢)。指定此屬性後,手勢與navigationBar的顯隱情況無關。

三:方法

1:push(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; })  跳轉

2:popN(n: number)  返回第N層

3:pop()   返回上一層

4:replaceAtIndex(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; }, index: number)  替換navigation棧的路由,索引指定要替換的堆棧中的路由。如果它是負面的,它從後面計數。

5:replace(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; }) 

替換當前頁面的路由,並立即載入新路由的視圖。

6:replacePrevious(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; }) 

替換上一頁的路由/視圖。

7:popToTop()  返回到頂層

8:popToRoute(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; }) 

返回特定路由對象的項目

9:replacePreviousAndPop(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; }) 

替換前面的路由/視圖並返回到它。

10:resetTo(route: { component: Function; title: string; passProps?: Object; backButtonTitle?: string; backButtonIcon?: Object; leftButtonTitle?: string; leftButtonIcon?: Object; onLeftButtonPress?: Function; rightButtonTitle?: string; rightButtonIcon?: Object; onRightButtonPress?: Function; wrapperStyle?: any; }) 

取代頂級和並執行poptotop

三:實例代碼

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TextInput,
  Alert,
  Image,
  TouchableHighlight,
  TouchableOpacity,
  NavigatorIOS,
  ScrollView
} from 'react-native';

//導航欄
class ReactNativeProject extends Component {
  render() {
    return (
      <NavigatorIOS style={{flex:1}} initialRoute={{ component:ListPage,title:'首頁'}}/>
    );
  }
}


//列表頁面
class ListPage extends Component {
  render(){
  return (
    <ScrollView style={styles.flex}>
      <Text style={styles.list_item} onPress={this._goToDetailPage.bind(this)}>訂單1詳情</Text>
      <Text style={styles.list_item} onPress={this._goToDetailPage.bind(this)}>訂單2詳情</Text>
      <Text style={styles.list_item} onPress={this._goToDetailPage.bind(this)}>訂單3詳情</Text>
    </ScrollView>
    );
}

_goToDetailPage(){
  this.props.navigator.push({
    component: DetailPage,
    title: '訂單詳情',
    rightButtonTitle: '購物車',
    onRightButtonPress: function(){
        alert('進入我的購物車');
      }
  });
}
}

//詳情頁
class DetailPage extends Component {
  _show(text) {
    alert(text);
  }

  _handleBackButtonPress() {
    this.props.navigator.pop();
  }

  render() {
    return (
      <View style={styles.container}>
              <TouchableOpacity
                 onPress={this._show.bind(this,'React Native')}
                 activeOpacity={0.5}>
                 <Text style={styles.item}>React Native</Text>
               </TouchableOpacity>

               <TouchableOpacity
                 onPress={this._handleBackButtonPress.bind(this)}>
                 <Text style={styles.item}>返回上一級頁面</Text>
               </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop:64
  },
  item:
  {
    fontSize:18,
    marginLeft:5,
    color:'#434343'
  },
  flex:{
    flex: 1,
  },
  list_item:{
    lineHeight:25,
    fontSize:16,
    marginLeft:10,
    marginRight:10
  }
});

AppRegistry.registerComponent('ReactNativeProject', () => ReactNativeProject);

效果圖:


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一個元素朗讀的內容可分為以下4個部分(4部分按先後順序朗讀) 1. Label:元素的標題 2. Value:元素的值(可選) 3. Traits:元素的特征,即類型,包含: 按鈕/鏈接/搜索框/鍵盤按鍵/圖片/播放音樂/選擇/總覺元素/頻繁更新/不可用空 4. Hint:提示(可選,與Traits ...
  • Android中Activity、Service、Fragment之間的相互通信比較麻煩,主要有以下一些方法: (1)使用廣播,發送者發出廣播,接收者接收廣播後進行處理; (2)使用Handler和Message,如下載線程完成下載任務後,給UI發送Message,UI收到Message後更新界面。 ...
  • 在Android開發中我們經常使用MediaPlayer來播放音頻文件,但是MediaPlayer存在一些不足,例如:資源占用量較高、延遲時間較長、不支持多個音頻同時播放等。這些缺點決定了MediaPlayer在某些場合的使用情況不會很理想,例如在對時間精準度要求相對較高的游戲開發中。 本文地址:h ...
  • MediaPlayer的狀態轉換圖也表徵了它的生命周期,如下: 這張狀態轉換圖清晰的描述了MediaPlayer的各個狀態,也列舉了主要的方法的調用時序,每種方法只能在一些特定的狀態下使用,如果使用時MediaPlayer的狀態不正確則會引發IllegalStateException異常。 Idle ...
  • 大多數android程式的右上角都會設置一個菜單按鈕比如微信的界面右上角的加號。 這個需要在layout同級目錄下新建文件夾命名為menu,再右擊新建的menu新建xml文件: xml文件代碼: 上面的兩個item代表代表的是你菜單欄上面有幾個按鈕。android:icon中可以自定義菜單項的圖片 ...
  • 今天用 cocos2dx3.11.1 + xcode7.3.1 進行開發,新建項目後遇到如下問題: 在iphone(ios9.3.5)真機運行項目(用xcode自帶虛擬機沒有出現問題) auto sprite = Sprite::create("HelloWorld.png"); 創建精靈後,spr ...
  • 本文調用android的媒體播放器實現一些音樂播放操作 項目佈局: 可以查看本地sdk中的文件查看相關api file:///……/sdk/docs/guide/topics/media/mediaplayer.html 本文地址:http://www.cnblogs.com/wuyudong/p/ ...
  • The file “FWLifeApp” couldn’t be opened because you don’t have permission to view it問題是因為項目文件中的Bundle identifier和Executable file的值不一致導致的。 解決前 解決後 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...