我正在尝试使用Twitter集成构建应用。当用户单击共享按钮时,此按钮应更改其图像。这已经有效但是当用户点击其他视图并最终再次使用共享按钮点击视图时...图像又是旧图像。我不知道如何解决这个问题,所以我帮你们可以给我一些建议:) 这是代码:
在我的.h文件中:
- (IBAction)postToTwitter:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *tweetBTN;
...在我的.m文件中:
- (void)viewDidLoad
{
//some code here
if ([[UIImage imageNamed:@"twitter02_dark.png"] isEqual:_tweetBTN.currentImage]) {
UIImage *twitterImage2 = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage2 forState:UIControlStateNormal];
}
else {
UIImage *twitterImage1 = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage1 forState:UIControlStateNormal];
}
}
- (IBAction)postToTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
//some code here
UIImage *twitterImage2 = [UIImage imageNamed:@"twitter02_dark.png"];
[_tweetBTN setImage:twitterImage2 forState:UIControlStateNormal];
}
}
如果有人能帮助我会很棒......
的更新: 的
我在答案中进行了修改......但我没有t get one thing: after first launching the app the image should be active (because it isn
“推文”)...
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"tweeted"]) {
UIImage *twitterImage = [UIImage imageNamed:@"twitter02_dark.png"];
[_tweetBTN setImage:twitterImage forState:UIControlStateNormal];
}
else {
UIImage *twitterImage = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage forState:UIControlStateNormal];
}
...但是图像很暗......为什么?之后这个if方法正常工作......对我来说很奇怪
答案 0 :(得分:0)
您可以尝试使用用户默认值来存储已按下按钮的事实
在postToTwitter方法中:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"tweeted"];
并删除viewDidLoad中的语句,并将其放在viewDidAppear中:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"tweeted"]) {
UIImage *twitterImage = [UIImage imageNamed:@"twitter02_dark.png"];
[_tweetBTN setImage:twitterImage forState:UIControlStateNormal];
}
else {
UIImage *twitterImage = [UIImage imageNamed:@"twitter02_active.png"];
[_tweetBTN setImage:twitterImage forState:UIControlStateNormal];
}
这将在应用程序启动之间持续存在,因此如果您退出应用程序然后返回,则按钮仍然会显示为黑色