iPhoneアプリ掲示板にコピーボタンを設置

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
bluedog
記事: 21
登録日時: 13年前

iPhoneアプリ掲示板にコピーボタンを設置

#1

投稿記事 by bluedog » 12年前

初めまして。bluedogです。

掲示板を開発しています。

投稿フォームからテキストを入力して掲示板へ反映させ、

掲示板にコピーボタンを配置して、そのテキストをコピーしたいとおもっています。

どのようにしたらコピーできるでしょうか。

現在は、storybordsを使用して、UITableviewcell内(掲示板)のコピーしたいテキスト(msg)にcopytfを繋げるとエラーになってしまいます。

コードは下記となります。

【JEViewController.h】

コード:

 

@interface JEViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
    IBOutlet UITableView *tblTest;
    NSMutableArray *_records;
    UILabel *lbl1;
    UILabel *lbl2;
}

@property (weak, nonatomic) IBOutlet UILabel *copytf;
- (IBAction)pushcopy:(id)sender;

@end

@interface NSString (URLEncoding)

- (NSString *)urlencode;
- (NSString *)urldecode;

@end

【JEViewController.m】

コード:


@interface JEViewController ()
@property (nonatomic) NSMutableArray *records;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *userTextField;
@property (weak, nonatomic) IBOutlet UITextField *msgTextField;

- (IBAction)refreshAction:(UIBarButtonItem *)sender;
- (IBAction)editAction:(UIBarButtonItem *)sender;
- (IBAction)searchAction:(UIButton *)sender;
- (IBAction)postAction:(UIButton *)sender;
- (void)requestGetUser:(NSString *)user;
- (void)requestPostUser:(NSString *)user msg:(NSString *)msg;
- (void)requestDeleteDocId:(NSString *)docId;

@end

@implementation JEViewController
@synthesize records = _records;
@synthesize copytf;


- (IBAction)pushcopy:(id)sender {
    UIPasteboard *board = [UIPasteboard generalPasteboard];
    [board setValue:copytf.text forPasteboardType:@"public.utf8-plain-text"];
    
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:copytf.text message:@"コピーしました" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [av show];
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    tblTest.delegate = self;
    tblTest.dataSource = self;
    
    
    [self requestGetUser:nil];
}

- (void)viewDidUnload
{
    [self setCopytf:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _records.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSDictionary *rec = [_records objectAtIndex:indexPath.row];
    NSString *user = [rec objectForKey:@"user"];
    NSString *msg = [rec objectForKey:@"msg"];
    
    lbl1 = (UILabel *)[cell viewWithTag:1];
    lbl2 = (UILabel *)[cell viewWithTag:2];
    
    lbl1.text = user;
    lbl2.text = msg;
    
    tableView.separatorColor = [UIColor clearColor];
    
    return cell;
}



開発は1年弱の経験となりますが掲示板は初めてなので、

ご教授いただけると助かります。

宜しくお願い致します。

アバター
h2so5
副管理人
記事: 2212
登録日時: 14年前
住所: 東京
連絡を取る:

Re: iPhoneアプリ掲示板にコピーボタンを設置

#2

投稿記事 by h2so5 » 12年前

bluedog さんが書きました: 現在は、storybordsを使用して、UITableviewcell内(掲示板)のコピーしたいテキスト(msg)にcopytfを繋げるとエラーになってしまいます。
この意味が分かりません。
UILabel *copytfとUITableViewCellの関連性が不明ですし、何をトリガーとして(IBAction)pushcopyを実行するのかも謎です。

bluedog
記事: 21
登録日時: 13年前

Re: iPhoneアプリ掲示板にコピーボタンを設置

#3

投稿記事 by bluedog » 12年前

h2so5さん返信ありがとうございます。

ViewにLabelを配置してcopytfを繋げるとLabelがコピーされるのですが、

フォームから投稿したUITableViewCell内のLabelにcopytfを繋げるとエラーになってしまいます。

ここまではこれたのですが、この先がわかりません…。

どのようにUITableViewCellとの関連性をつくり、(IBAction)pushcopyを実行すればよろしいでしょうか。

宜しくお願い致します。

しひ

Re: iPhoneアプリ掲示板にコピーボタンを設置

#4

投稿記事 by しひ » 12年前

UITableViewCell上のラベルは、JEViewControllerの制御下にないので、当然IBOutletで繋げることができません。
代替案として、セルを生成するときにaddTarget:action:forControlEventsメソッドでpushcopyを指定するのはどうでしょうか。

コード:

MyTableViewCell* cell = ...;
[cell.label addTarget:self action:@selector(pushcopy:) forControlEvents:UIControlEventTouchUpInside];
// ラベルではUIControlEventTouchUpInsideが使えなかったような気もしますけど

閉鎖

“C言語何でも質問掲示板” へ戻る