我有一个项目,显示与其他社交网络类似的状态Feed。每个Feed项都有几个按钮,可以执行不同的操作。其中一个按钮打开一个新的viewcontroller,显示已在特定状态上发布的注释。单击此按钮并打开视图控制器时,我希望它是推送segue,以便它们是后退按钮,用户可以导航回到源。
单击此按钮并启动新vc时,需要将有关所点击的特定状态/单元的一些唯一数据发送到“comments vc”。这样做的代码在哪里?
CUSTOM CELL .H
#import <UIKit/UIKit.h>
@interface FeedItemCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *DefaultImg;
@property (weak, nonatomic) IBOutlet UILabel *NameLabel;
@property (weak, nonatomic) IBOutlet UILabel *StatusLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (nonatomic, copy) NSString *msg_id;
@property (nonatomic, copy) NSString *status;
@property (nonatomic, weak) IBOutlet UIButton* commentButton;
@property (nonatomic, weak) IBOutlet UIButton* bumpButton;
@property (strong, nonatomic) id delegate;
-(IBAction)viewComments:(id)sender;
-(IBAction)bump:(id)sender;
@end
@protocol CustomCellProtocol <NSObject>
- (void)EBCellPressed:(NSString *)cellName;
CUSTOM CELL .M
#import "FeedItemCell.h"
#import "CommentsViewController.h"
#import "NSDate+TimeAgo.h"
@interface FeedItemCell() <WYPopoverControllerDelegate>
{
}
- (IBAction)open:(id)sender;
- (void)close:(id)sender;
@end
@implementation FeedItemCell
@synthesize commentButton;
- (instancetype)initWithDelegate:(id)delegate {
self = [super init];
if (self) {
self.delegate = delegate;
// Initialization code
}
return self;
}
-(IBAction)bump:(id)sender{
[self.delegate EBCellPressed:@"NAME"];
}
- (IBAction)open:(id)sender
{
}
@end
publicFeed。中号
#import "PublicFeedViewController.h"
#import "FeedItemCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "InboxDetailViewController.h"
#import "SWRevealViewController.h"
#import "CommentsViewController.h"
#import "NSDate+TimeAgo.h"
@interface PublicFeedViewController (){
NSArray *NameLabel;
NSArray *StatusLabel;
NSMutableArray *feedArray;
}
@end
@implementation PublicFeedViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//The below code prompts the user for push notifications. If allowed, code in AppDelegate takes over and stores the token.
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
// Do any additional setup after loading the view.
self.FeedTable.dataSource=self;
self.FeedTable.delegate=self;
// Set the side bar button action. When it's tapped, it'll show up the sidebar.
_sidebarButton.target = self.revealViewController;
_sidebarButton.action = @selector(revealToggle:);
// Set the gesture
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
[manager POST:@"www" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(@"JSON: %@", responseObject);
self->feedArray = [responseObject objectForKey:@"feed"];
[self.FeedTable reloadData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return feedArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *CellIdentifier=@"Cell";
FeedItemCell *Cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!Cell){
Cell = [[FeedItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSLog(@"FEED ARRAY: %@", self->feedArray);
NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row];
// Display recipe in the table cell
NSString *thumb_img = [tempDictionary objectForKey:@"thumb_img"];
NSString *thumb_path=@"http://buhzhyve.com/CI_REST_LOGIN/UPLOADS/thumbs/";
NSString *thumb_url = [thumb_path stringByAppendingString:thumb_img];
Cell.NameLabel.text=[tempDictionary objectForKey:@"first_name"];
Cell.StatusLabel.text=[tempDictionary objectForKey:@"message"];
Cell.msg_id=[tempDictionary objectForKey:@"msg_id"];
//Cell.status=[tempDictionary objectForKey:@"message"];
Cell.StatusLabel.lineBreakMode=0;
Cell.StatusLabel.numberOfLines=0;
NSString *commentCount = [tempDictionary objectForKey:@"comment_count"];
NSString *commentButtonText =[NSString stringWithFormat:@"Comments ( %@ )",commentCount];
[Cell.commentButton setTitle:commentButtonText forState: UIControlStateNormal];
NSString *bumpCount = [tempDictionary objectForKey:@"bump_count"];
NSString *bumpButtonText =[NSString stringWithFormat:@"Bumps ( %@ )",bumpCount];
[Cell.bumpButton setTitle:bumpButtonText forState: UIControlStateNormal];
//[Cell.StatusLabel sizeToFit];
NSString *created_string=[tempDictionary objectForKey:@"created"];
double created_double = created_string.doubleValue;
NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:created_double];
NSString *ago = [date timeAgo];
Cell.timeLabel.text=ago;
//Cell.DefaultImg.image = [UIImage imageNamed:@"buhz_mini_logo.png"];
[Cell.DefaultImg setImageWithURL:[NSURL URLWithString:thumb_url]
placeholderImage:[UIImage imageNamed:@"buhz_mini_logo.png"]];
return Cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Ideally you should do lazy loading so that instead of creating a new textView each time, you just reuse the same one.
UITextView *temp = [[UITextView alloc] initWithFrame:CGRectMake(82, 26, self.FeedTable.frame.size.width, 18)]; //This initial size doesn't matter
NSDictionary *tempDictionary= [self->feedArray objectAtIndex:indexPath.row];
NSString *status = [tempDictionary objectForKey:@"message"];
temp.font =[UIFont fontWithName:@"System" size:12];
temp.text = status;
CGFloat textViewWidth = 218;
CGRect tempFrame = CGRectMake(82,26,textViewWidth,18); //The height of this frame doesn't matter.
CGSize tvsize = [temp sizeThatFits:CGSizeMake(tempFrame.size.width, tempFrame.size.height)]; //This calculates the necessary size so that all the text fits in the necessary width.
//Add the height of the other UI elements inside your cell
return tvsize.height + 70;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"commentSegue"]) {
}
}
@end
publicfeed .h
#import <UIKit/UIKit.h>
@interface PublicFeedViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *FeedTable;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
- (IBAction)addItem;
@end
答案 0 :(得分:1)
因此,假设您在代码中创建了此按钮,这就是您可以处理此问题的方法。
第一行告诉按钮,当按下它时,需要调用此特定选择器/方法作为动作发送。
[button addTarget:self action:@selector(showNextViewController) forControlEvents:UIControlEventTouchUpInside];
然后你会在同一个类中创建这个方法。
- (void) showNextViewController
{
NewViewController *newViewController = [[NewViewController alloc] init]; //Edit this line of course to fit for your situation. I'm not sure if you're loading from an XIB or from a Storyboard, or neither.
newViewController.someVariable = someVariable;
newViewController.someOtherVariable = someOtherVariable;
[[[[[UIApplication sharedApplication] delegate] window] rootViewController].navigationController pushViewController:view animated:YES];
}
这会将必要的数据发送到新的视图控制器,并且还会在屏幕上显示带有后退按钮的新视图。
希望这有效!
答案 1 :(得分:0)
<强> 1。好的,假设这是你的自定义单元类。 在自定义单元格的.h文件中,您需要添加协议。
#import <UIKit/UIKit.h>
@interface CustomCell : UIView
@property (strong, nonatomic) id delegate; //this is used for sending messages out of the custom cell
//init
- (id)initWithFrame:(CGRect)frame andCatName:(NSString *)name andDelegate:(id)delegate;
@end
@protocol CustomCellProtocol <NSObject>
-(void)customCellSelected:(NSString *)cellName;
@end
我们实际上做的是创建一个类可以抛出的自定义事件,订阅它的每个人都可以在抛出customCellSelected
时运行一个方法。
<强> 2。现在,当您使用init
方法创建每个自定义单元格时,您需要提供一个委托,哪个类应该由自定义单元格将调用传递给customCellSelected
,因此在init方法中设置该委托强>
- (id)initWithFrame:(CGRect)frame andDelegate:(id)delegate {
self = [super initWithFrame:frame];
if (self) {
self.delegate = delegate; //setting which class should be called when calling protocol methods.
// Your initialization code
}
return self;
}
第3。现在在自定义单元格的.m文件中,当用户按下您的按钮并输入您的方法时,请将其设为buttonPressed
- (IBAction) buttonPressed:(id)sender {
[self.delegate customCellSelected:@"THE CELL'S NAME"]; // calling the protocol method.
}
现在对委托方法的调用应该转移到vc,因为当你创建自定义单元格时,你使用initWithFrame:(CGRect)frame andDelegate:(id)delegate
并转移self
作为委托,所以当[self.delegate customCellSelected:@"THE CELL'S NAME"];
是叫它实际上是在vc上调用的。
<强> 4。这是您在vc中创建自定义单元格的方式:
customCell *tempView = [[customCell alloc] initWithFrame:CGRectMake(X, Y, Width, Height) andDelegate:self]; // here you set the vc as the delegate
5.现在您要做的就是将方法customCellSelected
添加到您的vc代码中,以便在customCell调用它时调用它。
- (void)customCellSelected:(NSString *)cellName {
self.selectedCell = cellName;
[self performSegueWithIdentifier:@"SelectedCell" sender:self];
}
6.然后在vc中添加:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"SelectedCell"]) {
LevelSelectViewController *levelSelectViewController = (LevelSelectViewController *)segue.destinationViewController;
levelSelectViewController.cellName = self.selectedCell;
}
}
7.你必须要记住的事情就是从第一个vc到第二个vc创建一个segue: