单击“像所有视图一样”应显示与目标C相似 [英] Clicking on Like all the view should show like objective C

查看:87
本文介绍了单击“像所有视图一样”应显示与目标C相似的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择 Throwback -> Description 时,如果我单击 like 按钮,like按钮将变为 unlike >,并且 like count = 1099增加。如果我按回,我希望此不同的地方显示在 Throwback 旁边,在标签中说,然后再次选择 Throwback 按钮应显示不像,并且喜欢Count应该是1100。
请帮助我如何实现这一目标?





// DetailOfUser.m

  #impot DetailsOfStories.h 
@interface DetailOfUser()< UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray * arrayAboutList;
DetailsOfStories * viewController;
}

-(UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
静态NSString * cellIdentifier = @ cell ;
UILabel * title = [(UILabel *)cell viewWithTag:2];
title.text = [NSString stringWithFormat:@%@,[arrayAboutList [indexPath.row] valueForKey:@ title]] ;;

返回单元格;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


viewController = [self.storyboard InstantiateViewControllerWithIdentifier:@ DetailsOfStories];
viewController.descriptionList = [arrayAboutList [indexPath.row] mutableCopy];
[self.navigationController pushViewController:viewController animation:YES];


}
@end

// DetailsOfStories.m

  #import DetailOfUser.h 
@interface DetailsOfStories()
{
NSMutableArray * descriptionList;
BOOL按钮切换;
int number,number1;
NSNumber * num1;
}
@end
@implementation DetailsOfStories
@synthesize descriptionList;
-(void)viewDidLoad {
UILabel * like_count =(UILabel *)[self.view viewWithTag:3];
NSNumber * someNumber = @(数字);
NSString * someString = [someNumber stringValue];
like_count.text = someString;
}
-(IBAction)like:(id)sender {

if(!buttonToggled){
[sender setTitle:@ Unlike forState:UIControlStateNormal] ;
//数字是整数
数字= [num1 intValue] +1;
number1 =数字;
UILabel * like_count =(UILabel *)[self.view viewWithTag:3];
NSNumber * someNumber = @(数字);
NSString * someString = [someNumber stringValue];
like_count.text = someString;
buttonToggled = YES;
}
else {
[sender setTitle:@ Like forState:UIControlStateNormal];
数字1 =数字1-1;
UILabel * like_count =(UILabel *)[self.view viewWithTag:3];
NSNumber * someNumber = @(number1);
NSString * someString = [someNumber stringValue];
like_count.text = someString;
buttonToggled =否;
}
}


解决方案

您可以将喜欢号和状态封装到数据模型中。

两个控制器都可以访问和修改数据模型。在这种情况下,数据模型可以是 singleton ,以便您可以在控制器中获取它。

可能需要将数据同步到服务器或持久性数据稍后存储到本地存储中,数据模型可以封装所有这些服务。



以下是一些代码示例

  // YourDataModel 
@interface YourDataModel:NSObject

@property(非原子,分配)NSNumber * numbersOfLike;
@property(nonatomic,Assign)BOOL喜欢;

@end
@implemention
+(id)shareInstance {
static dispatch_once_t OnceToken;
静态YourDataModel *模型;
dispatch_once(& onceToken,^ {
model = [[YourDataModel alloc] init];
});
返回模型;
}

//可能会将数据同步到后端服务器。
-(void)sync {
}

//从本地存储或远程服务器加载类似的数字
-(instancetype)init {

}

//然后可以在控制器中使用它
[[YourDataModel shareInstance] like];
[[YourDataModel shareInstance] numberOfLike];
[[YourDataModel shareInstance] setLike:false];

更新:



如果要从详细信息页面更新您的Story ViewController。您可以在其 viewWillAppear()委托中更新赞状态。您可以在 Apple的官方文件中查看详细信息。 / p>

When I select Throwback -> Description occurs if I click on like button the like button changes to unlike and " like count=1099" increases. If I press back I want this unlike to be displayed next to Throwback let say in a label and again select Throwback button should display unlike and like Count should be 1100. please Help how can i achieve this ?

//DetailOfUser.m

    #impot"DetailsOfStories.h"    
    @interface DetailOfUser ()<UITableViewDelegate,UITableViewDataSource>
    {
      NSMutableArray *arrayAboutList;
        DetailsOfStories *viewController;
    }

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UILabel *title=[(UILabel *)cell viewWithTag:2];
    title.text=[NSString stringWithFormat:@"%@", [arrayAboutList[indexPath.row] valueForKey:@"title"]];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"DetailsOfStories"];
   viewController.descriptionList = [arrayAboutList[indexPath.row] mutableCopy];
    [self.navigationController pushViewController:viewController animated:YES];


}
@end

//DetailsOfStories.m

#import "DetailOfUser.h"
@interface DetailsOfStories ()
{
    NSMutableArray *descriptionList;
BOOL buttonToggled;
    int number,number1;
    NSNumber *num1;
}
@end
@implementation DetailsOfStories
@synthesize descriptionList;
- (void)viewDidLoad {
   UILabel *like_count=(UILabel *)[self.view viewWithTag:3];
    NSNumber *someNumber = @(number);
    NSString *someString = [someNumber stringValue];
    like_count.text=someString;
}
- (IBAction)like:(id)sender {

    if (!buttonToggled) {
        [sender setTitle:@"Unlike" forState:UIControlStateNormal];
        //number is interger
        number = [num1 intValue]+1;
        number1=number;
        UILabel *like_count=(UILabel *)[self.view viewWithTag:3];
        NSNumber *someNumber = @(number);
        NSString *someString = [someNumber stringValue];
        like_count.text=someString;
        buttonToggled = YES;
    }
    else {
        [sender setTitle:@"Like" forState:UIControlStateNormal];
        number1 = number1-1;
        UILabel *like_count=(UILabel *)[self.view viewWithTag:3];
        NSNumber *someNumber = @(number1);
        NSString *someString = [someNumber stringValue];
        like_count.text=someString;
        buttonToggled = NO;
    }
}

解决方案

You can encapsulate the 'likes' number and status into a data model.
Both your controllers can access and modify the data model. In this case, data model can be a singleton, so that you can acquire it in your controllers.
May be you need sync data to server or persistent data to local storage later, data model can encapsulate all this service.

Here is some code sample

// YourDataModel
@interface YourDataModel : NSObject

@property (nonatomic, assign) NSNumber *numbersOfLike;
@property (nonatomic, assign) BOOL like;

@end
@implemention 
+ (id)shareInstance {
  static dispatch_once_t onceToken;
  static YourDataModel *model;
  dispatch_once(&onceToken, ^{
     model = [[YourDataModel alloc] init];
  });
  return model;
}

// maybe sync data to backend server.
- (void)sync {
}

// load like numbers from local storage or remote server
- (instancetype)init {

}

// Then you can use it in your controllers
[[YourDataModel shareInstance] like];
[[YourDataModel shareInstance] numbersOfLike];
[[YourDataModel shareInstance] setLike:false];

Update:

If you want to update your story viewcontroller back from detail page. You can update 'like status' in its viewWillAppear() delegate. You can check details on Apple's official document.

这篇关于单击“像所有视图一样”应显示与目标C相似的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆