@@ -196,11 +196,7 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat
196
196
url_ = [webView.URL copy ];
197
197
198
198
[UIApplication sharedApplication ].networkActivityIndicatorVisible = NO ;
199
-
200
- NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
201
- UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh target: self action: @selector (refresh: )];
202
- [items replaceObjectAtIndex: 5 withObject: refreshButton];
203
- [self .navigationController.toolbar setItems: items];
199
+ [self updateToolbarItemsWithType: UIBarButtonSystemItemRefresh];
204
200
}
205
201
206
202
- (void )webView : (WKWebView *)webView didStartProvisionalNavigation : (WKNavigation *)navigation
@@ -220,21 +216,13 @@ - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation
220
216
}
221
217
222
218
[UIApplication sharedApplication ].networkActivityIndicatorVisible = YES ;
223
-
224
- NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
225
- UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemStop target: self action: @selector (stop: )];
226
- [items replaceObjectAtIndex: 5 withObject: refreshButton];
227
- [self .navigationController.toolbar setItems: items];
219
+ [self updateToolbarItemsWithType: UIBarButtonSystemItemStop];
228
220
}
229
221
230
222
- (void )webView : (WKWebView *)webView didFailNavigation : (WKNavigation *)navigation withError : (NSError *)error
231
223
{
232
224
[UIApplication sharedApplication ].networkActivityIndicatorVisible = NO ;
233
-
234
- NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
235
- UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh target: self action: @selector (refresh: )];
236
- [items replaceObjectAtIndex: 5 withObject: refreshButton];
237
- [self .navigationController.toolbar setItems: items];
225
+ [self updateToolbarItemsWithType: UIBarButtonSystemItemRefresh];
238
226
}
239
227
240
228
#pragma mark - UIWebViewDelegate
@@ -258,11 +246,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
258
246
url_ = [webView.request.URL copy ];
259
247
260
248
[UIApplication sharedApplication ].networkActivityIndicatorVisible = NO ;
261
-
262
- NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
263
- UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh target: self action: @selector (refresh: )];
264
- [items replaceObjectAtIndex: 5 withObject: refreshButton];
265
- [self .navigationController.toolbar setItems: items];
249
+ [self updateToolbarItemsWithType: UIBarButtonSystemItemRefresh];
266
250
}
267
251
268
252
- (void )webViewDidStartLoad : (UIWebView *)webView
@@ -282,21 +266,13 @@ - (void)webViewDidStartLoad:(UIWebView *)webView
282
266
}
283
267
284
268
[UIApplication sharedApplication ].networkActivityIndicatorVisible = YES ;
285
-
286
- NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
287
- UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemStop target: self action: @selector (stop: )];
288
- [items replaceObjectAtIndex: 5 withObject: refreshButton];
289
- [self .navigationController.toolbar setItems: items];
269
+ [self updateToolbarItemsWithType: UIBarButtonSystemItemStop];
290
270
}
291
271
292
272
- (void )webView : (UIWebView *)webView didFailLoadWithError : (NSError *)error
293
273
{
294
274
[UIApplication sharedApplication ].networkActivityIndicatorVisible = NO ;
295
-
296
- NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
297
- UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh target: self action: @selector (refresh: )];
298
- [items replaceObjectAtIndex: 5 withObject: refreshButton];
299
- [self .navigationController.toolbar setItems: items];
275
+ [self updateToolbarItemsWithType: UIBarButtonSystemItemRefresh];
300
276
}
301
277
302
278
- (BOOL )webView : (UIWebView *)webView shouldStartLoadWithRequest : (NSURLRequest *)request navigationType : (UIWebViewNavigationType)navigationType
@@ -348,4 +324,20 @@ - (NSString *)getFilePath:(NSString *)filename
348
324
return [NSString stringWithFormat: @" %@ /%@ " , [[NSBundle mainBundle ] pathForResource: @" M2DWebViewController" ofType: @" bundle" ], filename];
349
325
}
350
326
327
+ - (void )updateToolbarItemsWithType : (UIBarButtonSystemItem)type
328
+ {
329
+ if (type == UIBarButtonSystemItemRefresh) {
330
+ NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
331
+ UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemRefresh target: self action: @selector (refresh: )];
332
+ [items replaceObjectAtIndex: 5 withObject: refreshButton];
333
+ [self .navigationController.toolbar setItems: items];
334
+ }
335
+ else if (type == UIBarButtonSystemItemStop) {
336
+ NSMutableArray *items = [[self .navigationController.toolbar items ] mutableCopy ];
337
+ UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemStop target: self action: @selector (stop: )];
338
+ [items replaceObjectAtIndex: 5 withObject: refreshButton];
339
+ [self .navigationController.toolbar setItems: items];
340
+ }
341
+ }
342
+
351
343
@end
0 commit comments