質問編集履歴
1
基礎を見直すと解決できます。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,62 +1,3 @@
|
|
1
1
|
ボタンをタップすると、色が変わり、レルムでBool判定をし、カウントする、という処理をしたいです。
|
2
2
|
delegateを通して別のコントローラーでボタンを使用したいのですが、ボタンが反応しなくなってしまいました。
|
3
|
-
TweetCell.swiftではボタンと関数を定義しております。
|
3
|
+
TweetCell.swiftではボタンと関数を定義しております。
|
4
|
-
|
5
|
-
```ここに言語を入力
|
6
|
-
// TweetCell.swift
|
7
|
-
protocol TweetCellDelegate: class {
|
8
|
-
func handleLikeTapped(_ cell: TweetCell)
|
9
|
-
}
|
10
|
-
|
11
|
-
weak var delegate: TweetCellDelegate?
|
12
|
-
|
13
|
-
public lazy var likeButton: UIButton = {
|
14
|
-
let button = UIButton(type: .system)
|
15
|
-
button.setImage(UIImage(named: "like_unselected"), for: .normal)
|
16
|
-
button.tintColor = .darkGray
|
17
|
-
button.setDimensions(width: 20, height: 20)
|
18
|
-
button.addTarget(self, action: #selector(handleLikeTapped), for: .touchUpInside)
|
19
|
-
return button
|
20
|
-
}()
|
21
|
-
|
22
|
-
@objc func handleLikeTapped(){
|
23
|
-
// print("Likes Tapped")
|
24
|
-
delegate?.handleLikeTapped(self)
|
25
|
-
}
|
26
|
-
```
|
27
|
-
|
28
|
-
TweetCellのボタンをFeedControllerで使い、Viewには表示されているのですが、ボタンが反応しません。
|
29
|
-
しかし、TweetCellでdelegateを実行する前にprintで確認したら反応しましたが、別のコントローラーで使うと何も反応してくれません。
|
30
|
-
|
31
|
-
実行はできて、何のエラーも出てきません。
|
32
|
-
|
33
|
-
一体どこが間違えているのでしょうか?プロトコルの使い方を間違えていたりしてないでしょうか?
|
34
|
-
|
35
|
-
もしこのコードを見て分かる方がいたら、ご教授お願いします。
|
36
|
-
|
37
|
-
|
38
|
-
```ここに言語を入力
|
39
|
-
extension FeedController: TweetCellDelegate {
|
40
|
-
|
41
|
-
func handleLikeTapped(_ cell: TweetCell) {
|
42
|
-
print("Likes Tapped")
|
43
|
-
let indexPath = self.collectionView.indexPath(for: cell)
|
44
|
-
let tweetObject = tweets[indexPath!.row]
|
45
|
-
|
46
|
-
try! realm.write {
|
47
|
-
tweetObject.didLike.toggle()
|
48
|
-
if tweetObject.didLike {
|
49
|
-
cell.likeButton.tintColor = .lightGray
|
50
|
-
cell.likeButton.setImage(UIImage(named: "like_unselected"), for: .normal)
|
51
|
-
tweetObject.likes -= 1
|
52
|
-
realm.add(tweetObject, update: .all)
|
53
|
-
} else {
|
54
|
-
cell.likeButton.tintColor = .red
|
55
|
-
cell.likeButton.setImage(UIImage(named: "baseline_favorite_black_24pt_1x"), for: .normal)
|
56
|
-
tweetObject.likes += 1
|
57
|
-
realm.add(tweetObject, update: .all)
|
58
|
-
}
|
59
|
-
}
|
60
|
-
}
|
61
|
-
}
|
62
|
-
```
|