//// Copyright (c) 2015 Google Inc.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//#import "NewPostViewController.h"#import "User.h"#import "Post.h"@import Firebase;@interface NewPostViewController () <UITextFieldDelegate>@property (weak) IBOutlet UITextView *bodyTextView;@property (weak) IBOutlet UITextField *titleTextField;@end@implementation NewPostViewController#pragma mark - UIViewController lifecycle methods- (void)viewDidLoad {[super viewDidLoad];// [START create_database_reference]self.ref = [[FIRDatabase database] reference];// [END create_database_reference]UIToolbar *doneBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];doneBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Post"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(didTapShare:)];done.tintColor = [UIColor colorWithRed:1.0 green:143.0/255.0 blue:0.0 alpha:1.0];doneBar.items = @[flex, done, flex];[doneBar sizeToFit];_bodyTextView.inputAccessoryView = doneBar;_titleTextField.inputAccessoryView = doneBar;}- (IBAction)didTapShare:(id)sender {// [START single_value_read]NSString *userID = [FIRAuth auth].currentUser.uid;[[[_ref child:@"users"] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {// Get user valueUser *user = [[User alloc] initWithUsername:snapshot.value[@"username"]];// [START_EXCLUDE]// Write new post[self writeNewPost:userIDusername:user.usernametitle:self.titleTextField.textbody:self.bodyTextView.text];// Finish this Activity, back to the stream[self.navigationController popViewControllerAnimated:YES];// [END_EXCLUDE]} withCancelBlock:^(NSError * _Nonnull error) {NSLog(@"%@", error.localizedDescription);}];// [END single_value_read]}- (void)writeNewPost:(NSString *)userID username:(NSString *)username title:(NSString *)title body:(NSString *)body {// Create new post at /user-posts/$userid/$postid and at// /posts/$postid simultaneously// [START write_fan_out]NSString *key = [[_ref child:@"posts"] childByAutoId].key;NSDictionary *post = @{@"uid": userID,@"author": username,@"title": title,@"body": body};NSDictionary *childUpdates = @{[@"/posts/" stringByAppendingString:key]: post,[NSString stringWithFormat:@"/user-posts/%@/%@/", userID, key]: post};[_ref updateChildValues:childUpdates];// [END write_fan_out]}- (BOOL)textFieldShouldReturn:(UITextField *)textField {[textField resignFirstResponder];return NO;}@end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。