refreshControl附加到UIButton - 刷新UILabels

时间:2016-01-21 18:27:43

标签: ios objective-c uibutton uilabel uirefreshcontrol

我希望我的UILabels在用户发送新内存时自动刷新。我知道我可以做一次刷新,但我很好奇是否有可能将刷新的动作与sendMemories的IBAction或此代码中的最后一个UIAlertController联系起来。

我搜索了所有Apple文档和本网站,但我猜这是非常具体的,我不太了解refreshControl。

感谢您的任何想法!

这是ViewController.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "AppDelegate.h"



@interface MemoriesViewController : UIViewController <UITableViewDelegate, UITextViewDelegate>


@property (nonatomic, strong) PFQuery *query;
@property (nonatomic, strong) NSArray *mainArray;
@property (strong, nonatomic) IBOutlet UITextView *memoriesField;

@property (strong, nonatomic) IBOutlet UILabel *cellCountLabel;
@property (strong, nonatomic) IBOutlet UILabel *cellPhotoCountLabel;


- (IBAction)sendMemories:(UIButton *)sender;
- (IBAction)logOut:(id)sender;



@end

这是ViewController.m

#import "MemoriesViewController.h"
#import "MSCellAccessory.h"

@interface MemoriesViewController ()

@end

@implementation MemoriesViewController

@synthesize memoriesField = _memoriesField;
@synthesize cellCountLabel = _cellCountLabel;
@synthesize cellPhotoCountLabel = _cellPhotoCountLabel;


- (void)viewDidLoad {
    [super viewDidLoad];

    [self.navigationController.navigationBar setHidden:NO];

    _mainArray = [[NSArray alloc] initWithObjects:@"senderName", nil];

    self.memoriesField.delegate = self;

    PFUser *currentUser = [PFUser currentUser];
    if (currentUser) {
        NSLog(@"Current user: %@", currentUser.username);
    }
    else {
        [self performSegueWithIdentifier:@"showLogin" sender:self];
    }

    PFQuery *query = [PFQuery queryWithClassName:@"Memories"];
    [query whereKey:@"senderName" equalTo:[[PFUser currentUser] username]];
    [query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
        if (!error)
            NSLog (@"You have %d Memories Saved", count);
        self.cellCountLabel.text = [NSString stringWithFormat:@"You have %d Memories Saved", count];
    }];

    PFQuery *query2 = [PFQuery queryWithClassName:@"Photos"];
    [query2 whereKey:@"senderName" equalTo:[[PFUser currentUser] username]];
    [query2 countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
        if (!error)
                NSLog (@"You have %d Photos Saved", count);
        self.cellPhotoCountLabel.text = [NSString stringWithFormat:@"You have %d Photos Saved", count];
    }];
}




- (IBAction)sendMemories:(UIButton *)sender {

    //Place the loading spinner
    UIActivityIndicatorView *loadingSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    [loadingSpinner setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
    [loadingSpinner startAnimating];

    [self.view addSubview:loadingSpinner];

    PFObject *memories = [PFObject objectWithClassName:@"Memories"];
    memories[@"Memories"] = self.memoriesField.text;
    [memories setObject:[[PFUser currentUser] username] forKey:@"senderName"];
    [memories saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded){
            NSLog(@"succeeded");
            self.memoriesField.text = nil;
        }
        else{
            NSLog(@"problem");
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Our Apologies" message:@"Your Memory was not Recieved" preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok"
                                                                    style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];

            [alert addAction: defaultAction];
        }
            NSString *memories = [self.memoriesField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        [self.view endEditing:YES];

    if ([memories length] == 0)
    {
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Memory Recieved!" message:@"" preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"Ok"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {}];

        [alert addAction: defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else {
        [self.navigationController popToRootViewControllerAnimated:YES];
            }
        }];
    }

#pragma mark - log out method

- (IBAction)logOut:(id)sender {
    [PFUser logOut];
    [self performSegueWithIdentifier:@"showLogin" sender:self];
}


#pragma mark - UITextField Delegate Methods

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;

}
@end

0 个答案:

没有答案