#import "THSViewController.h" @interface THSViewController (Interface) - (void)addLabel; - (void)addButtonVoteUp; - (void)addButtonVoteDown; - (void)addButtonChuckWho; @end
- (void) addButtonVoteUp { UIButton *voteUpButton = [UIButton buttonWithType:UIButtonTypeSystem]; [voteUpButton setTitle:@"Vote Up" forState:UIControlStateNormal]; CGFloat x = self.view.frame.size.width / 2.0 - 50.0f; CGFloat y = self.view.frame.size.height / 2.0 + 0.0f; voteUpButton.frame = CGRectMake(x, y, 100.0f, 50.0f); [voteUpButton addTarget:self action:@selector(voteUp) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:voteUpButton]; } -(void) addButtonVoteDown { UIButton *voteDownButton = [UIButton buttonWithType:UIButtonTypeSystem]; [voteDownButton setTitle:@"Vote Down" forState:UIControlStateNormal]; CGFloat x = self.view.frame.size.width / 2.0 - 50.0f; CGFloat y = self.view.frame.size.height / 2.0 + 50.0f; voteDownButton.frame = CGRectMake(x, y, 100.0f, 50.0f); [voteDownButton addTarget:self action:@selector(voteDown) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:voteDownButton]; } - (void)addButtonChuckWho { UIButton *chuckWhoButton = [UIButton buttonWithType:UIButtonTypeSystem]; [chuckWhoButton setTitle:@"Chuck Who?" forState:UIControlStateNormal]; CGFloat x = self.view.frame.size.width / 2.0 - 50.0f; CGFloat y = self.view.frame.size.height / 2.0 + 150.0f; chuckWhoButton.frame = CGRectMake(x, y, 100.0f, 50.0f); [chuckWhoButton addTarget:self action:@selector(chuckWho) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:chuckWhoButton]; }
#import <UIKit/UIKit.h> @interface THSViewController : UIViewController @property (nonatomic, strong) UILabel *jokeLabel; - (void)voteUp; - (void)voteDown; - (void)chuckWho; @end
- (void)voteUp { } - (void)voteDown { } - (void)chuckWho { }
- (void)viewDidLoad { [super viewDidLoad]; [self addLabel]; [self addButtonVoteUp]; [self addButtonVoteDown]; [self addButtonChuckWho]; [self retrieveRandomJokes]; }
- (void)postURL:(NSURL *)url params:(NSDictionary *)params successBlock:(void(^)(NSData *))successBlock { self.successBlock = successBlock; // POST NSMutableArray *paramsArray = [NSMutableArray arrayWithCapacity:[params count]]; // key=value for (NSString *key in params) { [paramsArray addObject:[NSString stringWithFormat:@"%@=%@", key, params[key]]]; } // , , & NSString *postBodyString = [paramsArray componentsJoinedByString:@"&"]; // NSString NSData , NSData *postBodyData = [NSData dataWithBytes:[postBodyString UTF8String] length:[postBodyString length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; // POST [request setHTTPMethod:@"POST"]; // content-type form encoded [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"]; // POST [request setHTTPBody:postBodyData]; NSURLSessionConfiguration *conf = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:conf delegate:self delegateQueue:nil]; NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request]; [task resume]; }
@interface THSHTTPCommunication : NSObject <NSURLSessionDownloadDelegate> - (void)retrieveURL:(NSURL *)url successBlock:(void(^)(NSData *))successBlock; - (void)postURL:(NSURL *)url params:(NSDictionary *)params successBlock:(void(^)(NSData *))successBlock; @end
- (void)voteUp { NSURL *url = [NSURL URLWithString:@"http://example.com/rater/vote"]; THSHTTPCommunication *http = [[THSHTTPCommunication alloc] init]; NSDictionary *params = @{@"joke_id":jokeID, @"vote":@(1)}; [http postURL:url params:params successBlock:^(NSData *response) { NSLog(@"Voted Up"); }]; } - (void)voteDown { NSURL *url = [NSURL URLWithString:@"http://example.com/rater/vote"]; THSHTTPCommunication *http = [[THSHTTPCommunication alloc] init]; NSDictionary *params = @{@"joke_id":jokeID, @"vote":@(-1)}; [http postURL:url params:params successBlock:^(NSData *response) { NSLog(@"Voted Down"); }]; }
2015-11-08 14:51:20.724 Test[1248:80442] Voted Up
2015-11-08 14:51:57.896 Test[1273:81833] Voted Down
#import <UIKit/UIKit.h> @interface THSWebViewController : UIViewController @property (nonatomic, strong) UIWebView *webView; - (void)dismissView; // - (void)back; // - (void)forward; // @end
#import "THSWebViewController.h" #import "THSWebViewController+Interface.h" @interface THSWebViewController () @end @implementation THSWebViewController - (void)viewDidLoad { [super viewDidLoad]; [self addWebView]; [self addNavBar]; [self addTabBar]; } - (void)dismissView { // } - (void)back { // } - (void)forward { // } @end
#import "THSWebViewController.h" @interface THSWebViewController (Interface) - (void)addWebView; - (void)addNavBar; - (void)addTabBar; @end
#import "THSWebViewController+Interface.h" @implementation THSWebViewController (Interface) - (void)addWebView { self.webView = [[UIWebView alloc] initWithFrame:self.view.frame]; [self.view addSubview:self.webView]; } - (void)addNavBar { CGFloat width = self.view.frame.size.width; CGRect frame = CGRectMake(0.0f, 0.0f, width, 64.0f); UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:frame]; self.navigationItem.title = @"Chuck Norris"; [navBar pushNavigationItem:self.navigationItem animated:NO]; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(dismissView)]; [self.view addSubview:navBar]; } - (void)addTabBar { CGFloat width = self.view.frame.size.width; CGRect frame = CGRectMake(0.0f, self.view.frame.size.height - 44.0f, width, 44.0f); UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:frame]; frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f); UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithTitle:@"<" style:UIBarButtonItemStylePlain target:self action:@selector(back)]; UIBarButtonItem *forwardBtn = [[UIBarButtonItem alloc] initWithTitle:@">" style:UIBarButtonItemStylePlain target:self action:@selector(forward)]; [toolBar setItems:@[backBtn, forwardBtn]]; [self.view addSubview:toolBar]; } @end
#import "THSViewController.h" #import "THSViewController+Interface.h" #import "THSHTTPCommunication.h" #import "THSWebViewController.h" … - (void)chuckWho { THSWebViewController *webViewController = [[THSWebViewController alloc] init]; [self presentViewController:webViewController animated:YES completion:nil]; }
- (void)viewDidLoad { [super viewDidLoad]; [self addWebView]; [self addNavBar]; [self addTabBar]; // NSURLRequest URL NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://en.wikipedia.org/wiki/Chuck_Norris"]]; // webView [self.webView loadRequest:request]; }
- (void)dismissView { // webView , close [self dismissViewControllerAnimated:YES completion:nil]; } - (void)back { // , back [self.webView goBack]; } - (void)forward { // , forward [self.webView goForward]; }
webView:shouldStartLoadWithRequest:navigationType:
webViewDidStartLoad:
webViewDidFinishLoad:
webView:didFailLoadWithError:
Source: https://habr.com/ru/post/270429/
All Articles