Skip to content

Commit 87dfecf

Browse files
committed
add utility method
1 parent 4a47e26 commit 87dfecf

File tree

1 file changed

+22
-30
lines changed

1 file changed

+22
-30
lines changed

Pod/Classes/M2DWebViewController/M2DWebViewController.m

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,7 @@ - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigat
196196
url_ = [webView.URL copy];
197197

198198
[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];
204200
}
205201

206202
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
@@ -220,21 +216,13 @@ - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation
220216
}
221217

222218
[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];
228220
}
229221

230222
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
231223
{
232224
[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];
238226
}
239227

240228
#pragma mark - UIWebViewDelegate
@@ -258,11 +246,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView
258246
url_ = [webView.request.URL copy];
259247

260248
[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];
266250
}
267251

268252
- (void)webViewDidStartLoad:(UIWebView *)webView
@@ -282,21 +266,13 @@ - (void)webViewDidStartLoad:(UIWebView *)webView
282266
}
283267

284268
[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];
290270
}
291271

292272
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
293273
{
294274
[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];
300276
}
301277

302278
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
@@ -348,4 +324,20 @@ - (NSString *)getFilePath:(NSString *)filename
348324
return [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] pathForResource:@"M2DWebViewController" ofType:@"bundle"], filename];
349325
}
350326

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+
351343
@end

0 commit comments

Comments
 (0)