簡體   English   中英

按下UIButton時更改JSON數據鏈接

[英]Change JSON data link when UIButton is pressed

我有一個iPhone應用程序,當時我將JSON解析為表視圖並在自定義單元格中顯示數據。 我有三個包含相同結構但數據不同的JSON文件。 我還具有三個不同的UIButton,它們在同一頁面上顯示為選項卡欄,如下所示:

UIButtons(作為選項卡欄)

對於按鈕,我有兩種不同的圖像-一種是在選擇按鈕時,另一種是在未選擇按鈕時。

我的解析JSON文件的代碼如下所示:

#import "DEMOSecondViewController.h"
#import "DEMONavigationController.h"
#import "PostsObject.h"
#import "AFNetworking.h"

@interface DEMOSecondViewController ()

@end

@implementation DEMOSecondViewController
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView, movies = _movies;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"iCelebri.com";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:(DEMONavigationController *)self.navigationController
                                                                            action:@selector(showMenu)];

    self.tableView.separatorColor = [UIColor clearColor];

    // Setting Up Activity Indicator View
    self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityIndicatorView.hidesWhenStopped = YES;
    self.activityIndicatorView.center = self.view.center;
    [self.view addSubview:self.activityIndicatorView];
    [self.activityIndicatorView startAnimating];
    self.tableView.separatorColor = [UIColor clearColor];

    // Initializing Data Source
    self.movies = [[NSArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = JSON;
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];
}

// Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.movies && self.movies.count) {
        return self.movies.count;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 124;
}

- (void)tableView:(UITableView *)tableView
  willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setBackgroundColor:[UIColor clearColor]];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"PostsObject";

    PostsObject *cell = (PostsObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];

        self.tableView.backgroundView = imageView;
    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
    cell.title.text = [movie objectForKey:@"message"];
    cell.published.text = [movie objectForKey:@"published"];

    return cell;
}


@end

因此,例如當我單擊Twitter按鈕時,我想要NSURL *url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"]; 將鏈接更改為另一個。 它就像一個UISegmentControl,但帶有UIButtons。

這怎么可能?

謝謝。

首先連接到屬性上的tabBar上的3個UIButton,在示例中我將它們稱為:

btnFaceBook 
btnTwitter
btnTwitter2

為3個UIButton設置-viewDidLoad中該選項卡的URL,如下所示:

[btnFaceBook setTitle:@"http://URLFacebook.com" forState:UIControlStateDisabled];
[btnTwitter setTitle:@"http://URLTwitter.com" forState:UIControlStateDisabled];
[btnTwitter2 setTitle:@"http://URLTwitter2.com" forState:UIControlStateDisabled];

為3個UIButton設置“未按下”和“按下”的圖像,您可以從您的nib文件中執行此操作,也可以通過編程方式執行以下操作:

[btnFaceBook setBackgroundImage:[UIImage imageNamed:@"imageFacebookUnpressedUnSelected.png"] forState:UIControlStateNormal];
[btnFaceBook setBackgroundImage:[UIImage imageNamed:@"imageFacebookPressedSelected.png"] forState:UIControlStateSelected];

[btnTwitter setBackgroundImage:[UIImage imageNamed:@"imageTwitterUnpressedUnSelected.png"] forState:UIControlStateNormal];
[btnTwitter setBackgroundImage:[UIImage imageNamed:@"imageTwitterPressedSelected.png"] forState:UIControlStateSelected];

[btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"imageTwitter2UnpressedUnSelected.png"] forState:UIControlStateNormal];
[btnTwitter2 setBackgroundImage:[UIImage imageNamed:@"imageTwitter2PressedSelected.png"] forState:UIControlStateSelected];

然后將所有3個UIButton的TouchUpInside操作連接到此方法:

- (IBAction)btnFromTabBarClicked:(UIButton *)sender
{
    //Unselect all 3 buttons
    btnFaceBook.selected = btnTwitter.selected = btnTwitter2.selected = NO;

    //Select the button that was clicked        
    sender.selected = YES;

    //Set the string of an NSMutableString property called strURLToLoad with the URL
    //The URL is pre stored in the text of the UIButton in the Disabled text.
    [strURLToLoad setString:[sender titleForState:UIControlStateDisabled]];

    //Load the URL
    [self loadJSONFromCurrentURL];
}

您可以簡單地為NSURL創建一個屬性,以便可以從所有類方法中訪問它。 像這樣修改您的代碼:

@interface DEMOSecondViewController ()
@property (strong) NSURL *url;
@end

然后在您的viewDidLoad中:

url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];

然后為您的按鈕創建一個動作:

// Facebook button example. Do the same for the other 2 buttons
- (IBAction)facebookButton:(id)sender {
    url = [NSURL URLWithString:@"yourNewUrl"];
    // Here you need to call again your AFNewtorking code (maybe put it in a sperate method instead of the viewDidLoad)
}

我希望我能很好地解釋答案:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM