回答編集履歴

1

修正

2016/08/27 07:01

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -63,3 +63,83 @@
63
63
 
64
64
 
65
65
  ```
66
+
67
+
68
+
69
+ メソッドの中で他のメソッドを呼ぶには、呼び出す前にメソッドを定義していないとだめなので、先頭で定義して呼び出し時に`self`を付けないと以下のように使用できます。
70
+
71
+
72
+
73
+ ```swift
74
+
75
+ func jumpTourl(urlString: String){
76
+
77
+
78
+
79
+ func showAlert(message: String){
80
+
81
+ // >= ios8
82
+
83
+ let alertController = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
84
+
85
+ let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
86
+
87
+ alertController.addAction(defaultAction)
88
+
89
+ self.presentViewController(alertController, animated: true, completion: nil)
90
+
91
+ }
92
+
93
+
94
+
95
+
96
+
97
+ if let url = NSURL(string: urlString){
98
+
99
+ let urlRequest = NSURLRequest(URL: url)
100
+
101
+ self.webView.loadRequest(urlRequest)
102
+
103
+ } else {
104
+
105
+ showAlert("Invalid URL")
106
+
107
+ }
108
+
109
+
110
+
111
+
112
+
113
+ var urlString = self.textField.text
114
+
115
+ urlString = urlString!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
116
+
117
+ if urlString == "" {
118
+
119
+ // alert
120
+
121
+ showAlert("Please enter URL")
122
+
123
+ } else {
124
+
125
+ // jumpTourl
126
+
127
+ self.jumpTourl(urlString!)
128
+
129
+ self.setupButtonsEnabled()
130
+
131
+ }
132
+
133
+ self.textField.resignFirstResponder()
134
+
135
+ return true
136
+
137
+ }
138
+
139
+
140
+
141
+ ```
142
+
143
+
144
+
145
+