跳到主要内容
新架构实战课 实操 + 基建 + 原理全维度包揽,抢先掌握 React Native 新架构精髓 立即查看 >

AlertIOS

AlertIOS用于弹出一个 iOS 提示对话框,可以通知用户一些信息或是提示用户输入一些文字。

弹出一个 iOS 提示框:

AlertIOS.alert(
'Sync Complete',
'All your data are belong to us.'
);

弹出一个带输入框的 iOS 提示框:

AlertIOS.prompt(
'Enter a value',
null,
text => console.log("You entered "+text)
);

其他情况下,尤其是仅仅显示一个静态的提示框时,应该使用跨平台的Alert


文档

方法

alert()

static alert(title: string, [message]: string, [callbackOrButtons]: ?(() => void), ButtonsArray, [type]: AlertType): [object Object]

创建并显示一个提示框。

参数:

名称类型必需说明
titlestringThe dialog's title. Passing null or '' will hide the title.
messagestringAn optional message that appears below the dialog's title.
callbackOrButtons?(() => void),ButtonsArrayThis optional argument should be either a single-argument function or an array of buttons. If passed a function, it will be called when the user taps 'OK'. If passed an array of button configurations, each button should include a text key, as well as optional onPress and style keys. style should be one of 'default', 'cancel' or 'destructive'.
类型AlertTypeDeprecated, do not use.

Example with custom buttons:

AlertIOS.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()

static prompt(title: string, [message]: string, [callbackOrButtons]: ?((text: string) => void), ButtonsArray, [type]: AlertType, [defaultValue]: string, [keyboardType]: string): [object Object]

Create and display a prompt to enter some text.

参数:

名称类型必需说明
titlestringThe dialog's title.
messagestringAn optional message that appears above the text input.
callbackOrButtons?((text: string) => void),ButtonsArrayThis optional argument should be either a single-argument function or an array of buttons. If passed a function, it will be called with the prompt's value when the user taps 'OK'. If passed an array of button configurations, each button should include a text key, as well as optional onPress and style keys (see example). style should be one of 'default', 'cancel' or 'destructive'.
类型AlertTypeThis configures the text input. One of 'plain-text', 'secure-text' or 'login-password'.
defaultValuestringThe default text in text input.
keyboardTypestringThe keyboard type of first text field(if exists). One of 'default', 'email-address', 'numeric', 'phone-pad', 'ascii-capable', 'numbers-and-punctuation', 'url', 'number-pad', 'name-phone-pad', 'decimal-pad', 'twitter' or 'web-search'.

Example with custom buttons:

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',
);

,

Example with the default button and a custom callback:

AlertIOS.prompt(
'Update username',
null,
text => console.log('Your username is ' + text),
null,
'default',
);

类型定义

AlertType

An Alert button type

类型
$Enum

常量:

Value说明
defaultDefault alert with no inputs
plain-textPlain text input alert
secure-textSecure text input alert
login-passwordLogin and password alert

AlertButtonStyle

An Alert button style

类型
$Enum

常量:

Value说明
defaultDefault button style
cancelCancel button style
destructiveDestructive button style

ButtonsArray

Array or buttons

类型
Array

属性:

名称类型说明
[text]stringButton label
[onPress]functionCallback function when button pressed
[style]AlertButtonStyleButton style

常量:

Value说明
textButton label
onPressCallback function when button pressed
styleButton style