iOS 警告

利用一个特定的标题和消息来启动一个警告对话框。

提供一个可选按钮的列表。点击任何按钮触发各自的按下回调动作,并且忽略警告。在默认情况下,只有一个按钮是“OK”按钮。列表中最后一个按钮被视为“主”按钮,它被用粗体显示出来了。

  1. AlertIOS.alert(
  2. 'Foo Title',
  3. 'My Alert Msg',
  4. [
  5. {text: 'Foo', onPress: () => console.log('Foo Pressed!')},
  6. {text: 'Bar', onPress: () => console.log('Bar Pressed!')},
  7. ]
  8. )}

方法

  1. static alert(title: string, message?: string, buttons?: Array<{ text: ?string; onPress: ?Function; }>)

例子

Edit on GitHub

  1. 'use strict';
  2. var React = require('react-native');
  3. var {
  4. StyleSheet,
  5. View,
  6. Text,
  7. TouchableHighlight,
  8. AlertIOS,
  9. } = React;
  10. exports.framework = 'React';
  11. exports.title = 'AlertIOS';
  12. exports.description = 'iOS alerts and action sheets';
  13. exports.examples = [{
  14. title: 'Alerts',
  15. render() {
  16. return (
  17. <View>
  18. <TouchableHighlight style={styles.wrapper}
  19. onPress={() => AlertIOS.alert(
  20. 'Foo Title',
  21. 'My Alert Msg'
  22. )}>
  23. <View style={styles.button}>
  24. <Text>Alert with message and default button</Text>
  25. </View>
  26. </TouchableHighlight>
  27. <TouchableHighlight style={styles.wrapper}
  28. onPress={() => AlertIOS.alert(
  29. null,
  30. null,
  31. [
  32. {text: 'Button', onPress: () => console.log('Button Pressed!')},
  33. ]
  34. )}>
  35. <View style={styles.button}>
  36. <Text>Alert with only one button</Text>
  37. </View>
  38. </TouchableHighlight>
  39. <TouchableHighlight style={styles.wrapper}
  40. onPress={() => AlertIOS.alert(
  41. 'Foo Title',
  42. 'My Alert Msg',
  43. [
  44. {text: 'Foo', onPress: () => console.log('Foo Pressed!')},
  45. {text: 'Bar', onPress: () => console.log('Bar Pressed!')},
  46. ]
  47. )}>
  48. <View style={styles.button}>
  49. <Text>Alert with two buttons</Text>
  50. </View>
  51. </TouchableHighlight>
  52. <TouchableHighlight style={styles.wrapper}
  53. onPress={() => AlertIOS.alert(
  54. 'Foo Title',
  55. null,
  56. [
  57. {text: 'Foo', onPress: () => console.log('Foo Pressed!')},
  58. {text: 'Bar', onPress: () => console.log('Bar Pressed!')},
  59. {text: 'Baz', onPress: () => console.log('Baz Pressed!')},
  60. ]
  61. )}>
  62. <View style={styles.button}>
  63. <Text>Alert with 3 buttons</Text>
  64. </View>
  65. </TouchableHighlight>
  66. <TouchableHighlight style={styles.wrapper}
  67. onPress={() => AlertIOS.alert(
  68. 'Foo Title',
  69. 'My Alert Msg',
  70. '..............'.split('').map((dot, index) => ({
  71. text: 'Button ' + index,
  72. onPress: () => console.log('Pressed ' + index)
  73. }))
  74. )}>
  75. <View style={styles.button}>
  76. <Text>Alert with too many buttons</Text>
  77. </View>
  78. </TouchableHighlight>
  79. </View>
  80. );
  81. },
  82. }];
  83. var styles = StyleSheet.create({
  84. wrapper: {
  85. borderRadius: 5,
  86. marginBottom: 5,
  87. },
  88. button: {
  89. backgroundColor: '#eeeeee',
  90. padding: 10,
  91. },
  92. });