[英]Change JSON data link when UIButton is pressed
I have an iPhone app were I parse JSON to a Table View and display the data in a custom cell. 我有一个iPhone应用程序,当时我将JSON解析为表视图并在自定义单元格中显示数据。 I have three different JSON files containing the same structure but with different data. 我有三个包含相同结构但数据不同的JSON文件。 I also have three different UIButtons displaying as a tab bar on the same page, like this: 我还具有三个不同的UIButton,它们在同一页面上显示为选项卡栏,如下所示:
I have two different images for the buttons - one when it's selected and one when it's unselected. 对于按钮,我有两种不同的图像-一种是在选择按钮时,另一种是在未选择按钮时。
My code for parsing the JSON file looks like this: 我的解析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
So when I click for example the Twitter button, I want the NSURL *url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
因此,例如当我单击Twitter按钮时,我想要NSURL *url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
to change the link to another. 将链接更改为另一个。 It's like a UISegmentControl but with UIButtons instead. 它就像一个UISegmentControl,但带有UIButtons。
How is this possible? 这怎么可能?
Thanks. 谢谢。
First connect to a property the 3 UIButtons on the tabBar I called them for the example: 首先连接到属性上的tabBar上的3个UIButton,在示例中我将它们称为:
btnFaceBook
btnTwitter
btnTwitter2
Set for the 3 UIButtons the URL for that tab in -viewDidLoad like this: 为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];
Set for the 3 UIButtons the images for Unpressed and pressed, You can do that from your nib file or like that programatically: 为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];
Then connect all 3 UIButton's TouchUpInside action to this method: 然后将所有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];
}
You can simply create a property for your NSURL so you can access it from all your class methods. 您可以简单地为NSURL创建一个属性,以便可以从所有类方法中访问它。 Modify your code like this: 像这样修改您的代码:
@interface DEMOSecondViewController ()
@property (strong) NSURL *url;
@end
Then in your viewDidLoad: 然后在您的viewDidLoad中:
url = [[NSURL alloc] initWithString:@"http://my-site.com/jsonfile.php?name=Name"];
Then create an action for your buttons: 然后为您的按钮创建一个动作:
// 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)
}
I hope I well explained the answer :) 我希望我能很好地解释答案:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.