[英]UIButton configuration with highlight
我已經創建了一個UIbutton
(在情節UIbutton
中)以將一個對象添加到收藏夾列表中,但是在配置它時遇到了麻煩。 這會將對象添加到收藏夾,但是如何取消收藏呢?
我已經搜索和思考了一段時間,我的想法是在manageHighlightAndFave中創建一個if語句,例如:如果favButton state = highlighted
,請從收藏夾中刪除並刪除突出顯示。 否則,添加到收藏夾並添加突出顯示。 這好嗎,或者做我想做的事的首選方式是什么? 我喜歡一個例子,因為我是編程新手。
-(IBAction)favoriteButtonPressed:(id)sender
{
[self performSelector:@selector(manageHighlightAndFave:) withObject:sender afterDelay:0];
}
- (void)manageHighlightAndFave:(UIButton*)favButton {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected"
object:selectedObject];
[favButton setHighlighted:YES];
}
PS。 與情節提要中的“觸地得分”鏈接。
我建議您制作一個自定義按鈕。 以下參考代碼。
首先,點擊以下按鈕。 文件新文件可可觸摸目標C類 收藏按鈕
#import <UIKit/UIKit.h>
@interface FavoriteButton : UIButton
@property (nonatomic, assign) BOOL isFavorite;
@end
收藏按鈕
#import "FavoriteButton.h"
@implements FavoriteButton : UIButton
@synthesize isFavorite;
...
@end
其次,在情節提要中鏈接一個FavoriteButton。 請參考下圖。 在情節提要板上的右側面板中。 之前,點擊了您原來的UIButton
YourViewController.h
#import <UIKit/UIKit.h>
#import "FavoriteButton.h"
@interface YourViewController : UIViewController
@property (retain, nonatomic) IBOutlet FavoriteButton *favoriteButton;
@end
@implements YourViewController : UIViewController
@synthesize favoriteButton;
-(void) viewDidLoad
{
self.favoriteButton = [[FavoriteButton alloc] initWithFrame:...]];
//favoriteButton.isFavorite = NO; (already set in storyboard)
...
}
-(IBAction)favoriteButtonPressed:(id)sender
{
[self performSelector:@selector(manageHighlightAndFave:) withObject:sender afterDelay:0];
}
- (void)manageHighlightAndFave:(FavoriteButton *)favButton {
[[NSNotificationCenter defaultCenter] postNotificationName:@"ItemSelected"
object:selectedObject];
//true-false(YES-NO) Toggled. but isFavorite property is Must be initialized to false.
favButton.isFavorite = !favButton.isFavorite;
if(favButton.isFavorite)
{
favButton.imageView.image = [UIImage imageNamed:@"your_star_image"];
[favButton setHighlighted:YES];
}
else
{
favButton.imageView.image = nil;
[favButton setHighlighted:NO];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.