简体   繁体   English

如何将单击的最新按钮标签添加到数组中

[英]How to add the tag of latest button clicked into the array

I have created 5 views using Page view controller. 我已经使用页面视图控制器创建了5个视图。 Each view has 4 buttons. 每个视图都有4个按钮。 When I click any button, the tag of that button gets added to an array. 当我单击任何按钮时,该按钮的标签将添加到数组中。 Now, the situation is..Say, I opened view 1..I clicked a button..the button's tag gets added to the array. 现在,情况是..说,我打开了视图1 ..我单击了一个按钮..该按钮的标签已添加到数组中。 when I click another button in the same view, this button's tag also gets added to the array. 当我在同一视图中单击另一个按钮时,该按钮的标签也会添加到数组中。 The task is to remove the previously clicked button's tag from the array and only the latest one should persist. 该任务是从阵列中删除先前单击的按钮的标记,并且仅应保留最新的标记。 Any help? 有什么帮助吗? Thank You 谢谢

I suggest you to use a dictionary instead of array. 我建议您使用字典而不是数组。 That way you can easily replace the object related to a specific key. 这样,您可以轻松替换与特定键相关的对象。

[dictionary setObject:object key:somekey];

you better keep the tag values in NSMutableSet object which will help you in avoiding duplicates. 您最好将标记值保留在NSMutableSet对象中,这将有助于您避免重复。

you directly add button object to NSMutableSet and check if the button is available in NSMutableSet remove the button from NSMutableSet else add it to the NSMutableSet 您可以直接将按钮对象添加到NSMutableSet中,并检查该按钮在NSMutableSet中是否可用,然后从NSMutableSet中删除该按钮,否则将其添加到NSMutableSet中

NSMutableSet *set;
    [set addObject:senderButton];
    if([set containsObject:senderButton]) {
     //set contains button remove that button
    } else {
        // set doesn't contains button add it
    }

Add null objects intially in NSMutableDictionary to check whether button clicked or not NSMutableDictionaryNSMutableDictionary添加null对象,以检查按钮是否被单击

//number depends on numbers of view in page view controller
for(int i=0; i<[yourPageViewController.viewControllers; i++)
    [yourMutDictionary setObject:[NSNull null] forKey:[NSString stringWithFormat:@"%d",i]];

UIButton's click event will be contain below lines of code UIButton's click event将包含在下面的代码行中

-(IBAction)btnClickedAction:(id)sender
{
   UIButton *btnClicked = (UIButton *)sender;
   NSUInteger index = yourPageViewController.pageControl.currentPage
   [yourMutDictionary setObject:[NSString stringWithFormat:@"%d",btnClicked.tag] forKey:[NSString stringWithFormat:@"%i",index]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM