进阶 reactnative.dev 2026-10-11 06:04:21 · 38 阅读
第5章 React Native:已移除 AlertIOS,迁移至跨平台 Alert
版本:0.77❌ AlertIOS
已移除,请改用 Alert。
AlertIOS 用于创建带消息的 iOS 弹窗,或创建供用户输入的输入框。
创建 iOS 弹窗:
React JSXAlertIOS.alert(
'Sync Complete',
'All your data are belong to us.',
);
创建 iOS 输入框: React JSXAlertIOS.prompt('Enter a value', null, text =>
console.log('You entered ' + text),
);
如果不需要 iOS 专属的输入框,建议使用跨平台的 Alert.alert 方法。 Reference 方法 alert() React JSXstatic alert(title: string, [message]: string, [callbackOrButtons]: ?(() => void), ButtonsArray, [type]: AlertType): [object Object]
创建并显示弹窗。 参数: 名称类型必填描述titlestring是对话框标题,传入 null 或 '' 将隐藏标题。messagestring否可选的消息内容,显示在标题下方。callbackOrButtons?(() => void),ButtonsArray否可选参数,可以是一个单参数函数或按钮配置数组。传入函数时,用户点击'OK'后会调用该函数;传入数组时,每个按钮配置应包含 text 键,以及可选的 onPress 和 style 键。style 可取 'default'、'cancel' 或 'destructive'。typeAlertType否已废弃,请勿使用。 自定义按钮的示例: React JSXAlertIOS.alert(
'Update available',
'Keep your app up to date to enjoy the latest features',
[
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: 'Install',
onPress: () => console.log('Install Pressed'),
},
],
);
prompt() React JSXstatic prompt(title: string, [message]: string, [callbackOrButtons]: ?((text: string) => void), ButtonsArray, [type]: AlertType, [defaultValue]: string, [keyboardType]: string): [object Object]
创建并显示一个输入文本的提示框。 参数: 名称 类型 是否必填 描述 title string 是 对话框的标题。message string 否 可选消息,显示在文本输入框上方。callbackOrButtons ((text: string) => void) | ButtonsArray 否 此可选参数应为单参数函数或按钮数组。若传入函数,当用户点击「确定」时将带着提示框的值被调用。若传入按钮配置数组,每个按钮应包含 text 键,以及可选的 onPress 和 style 键(参见示例)。style 应为 'default'、'cancel' 或 'destructive' 之一。type AlertType 否 配置文本输入类型。'plain-text'、'secure-text' 或 'login-password' 之一。defaultValue string 否 文本输入框的默认文本。keyboardType string 否 首个文本字段的键盘类型(如果存在)。'default'、'email-address'、'numeric'、'phone-pad'、'ascii-capable'、'numbers-and-punctuation'、'url'、'number-pad'、'name-phone-pad'、'decimal-pad'、'twitter' 或 'web-search' 之一。 自定义按钮示例: React JSX AlertIOS.prompt( 'Enter password', 'Enter your password to claim your $1.5B in lottery winnings', [ { text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel', }, { text: 'OK', onPress: password => console.log('OK Pressed, password: ' + password), }, ], 'secure-text', ); , 使用默认按钮和自定义回调示例: React JSX AlertIOS.prompt( 'Update username', null, text => console.log('Your username is ' + text), null, 'default', ); 类型定义 AlertType Alert 按钮类型 类型 $Enum 常量: 值 描述 default 无输入框的默认 Alert plain-text 纯文本输入框 Alert secure-text 安全文本输入框 Alert login-password 登录和密码 Alert AlertButtonStyle Alert 按钮样式 类型 $Enum 常量: 值 描述 default 默认按钮样式 cancel 取消按钮样式 destructive 破坏性按钮样式 ButtonsArray 按钮数组 类型 Array 属性: 名称 类型 描述 [text] string 按钮标签 [onPress] function 按钮点击时的回调函数 [style] AlertButtonStyle 按钮样式 常量: 值 描述 text 按钮标签 onPress 按钮点击时的回调函数 style 按钮样式
'Sync Complete',
'All your data are belong to us.',
);
创建 iOS 输入框: React JSXAlertIOS.prompt('Enter a value', null, text =>
console.log('You entered ' + text),
);
如果不需要 iOS 专属的输入框,建议使用跨平台的 Alert.alert 方法。 Reference 方法 alert() React JSXstatic alert(title: string, [message]: string, [callbackOrButtons]: ?(() => void), ButtonsArray, [type]: AlertType): [object Object]
创建并显示弹窗。 参数: 名称类型必填描述titlestring是对话框标题,传入 null 或 '' 将隐藏标题。messagestring否可选的消息内容,显示在标题下方。callbackOrButtons?(() => void),ButtonsArray否可选参数,可以是一个单参数函数或按钮配置数组。传入函数时,用户点击'OK'后会调用该函数;传入数组时,每个按钮配置应包含 text 键,以及可选的 onPress 和 style 键。style 可取 'default'、'cancel' 或 'destructive'。typeAlertType否已废弃,请勿使用。 自定义按钮的示例: React JSXAlertIOS.alert(
'Update available',
'Keep your app up to date to enjoy the latest features',
[
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel',
},
{
text: 'Install',
onPress: () => console.log('Install Pressed'),
},
],
);
prompt() React JSXstatic prompt(title: string, [message]: string, [callbackOrButtons]: ?((text: string) => void), ButtonsArray, [type]: AlertType, [defaultValue]: string, [keyboardType]: string): [object Object]
创建并显示一个输入文本的提示框。 参数: 名称 类型 是否必填 描述 title string 是 对话框的标题。message string 否 可选消息,显示在文本输入框上方。callbackOrButtons ((text: string) => void) | ButtonsArray 否 此可选参数应为单参数函数或按钮数组。若传入函数,当用户点击「确定」时将带着提示框的值被调用。若传入按钮配置数组,每个按钮应包含 text 键,以及可选的 onPress 和 style 键(参见示例)。style 应为 'default'、'cancel' 或 'destructive' 之一。type AlertType 否 配置文本输入类型。'plain-text'、'secure-text' 或 'login-password' 之一。defaultValue string 否 文本输入框的默认文本。keyboardType string 否 首个文本字段的键盘类型(如果存在)。'default'、'email-address'、'numeric'、'phone-pad'、'ascii-capable'、'numbers-and-punctuation'、'url'、'number-pad'、'name-phone-pad'、'decimal-pad'、'twitter' 或 'web-search' 之一。 自定义按钮示例: React JSX AlertIOS.prompt( 'Enter password', 'Enter your password to claim your $1.5B in lottery winnings', [ { text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel', }, { text: 'OK', onPress: password => console.log('OK Pressed, password: ' + password), }, ], 'secure-text', ); , 使用默认按钮和自定义回调示例: React JSX AlertIOS.prompt( 'Update username', null, text => console.log('Your username is ' + text), null, 'default', ); 类型定义 AlertType Alert 按钮类型 类型 $Enum 常量: 值 描述 default 无输入框的默认 Alert plain-text 纯文本输入框 Alert secure-text 安全文本输入框 Alert login-password 登录和密码 Alert AlertButtonStyle Alert 按钮样式 类型 $Enum 常量: 值 描述 default 默认按钮样式 cancel 取消按钮样式 destructive 破坏性按钮样式 ButtonsArray 按钮数组 类型 Array 属性: 名称 类型 描述 [text] string 按钮标签 [onPress] function 按钮点击时的回调函数 [style] AlertButtonStyle 按钮样式 常量: 值 描述 text 按钮标签 onPress 按钮点击时的回调函数 style 按钮样式