ios - 在表格 View 中保存选定的行并返回主屏幕 UIButtons

标签 ios iphone objective-c ios4 xcode4.2

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

8年前关闭。




Improve this question




Main Screen

Table View select specific Row and save that row

Show specific row contents on the main screen

Plz code Help 谁能告诉我如何完成这项任务?
在主屏幕中,用户选择足球运动员,在表格 View 单元格的第二个屏幕中,用户选择特定行并保存该行并返回主视图。在主视图中,它会显示特定行的视频。
基本上我想知道特定的行选择,将该选择保存在表格 View 中并在主屏幕中显示他们的内容。

最佳答案

通过下面的代码,它实现了委托(delegate)概念,也实现了你的问题的解决方案希望这对你有帮助:)

  //in your main view controller

  #import "ViewController.h"
  #import "FootBallPlayersViewController.h"

  @interface ViewController ()<FootballPlayerDelegate>//confirms to this delegate 

  @end

  @implementation ViewController

  - (void)viewDidLoad
   {
        [super viewDidLoad];



// Do any additional setup after loading the view, typically from a nib.
   }

 - (IBAction)whenSelectButtonClicked:(id)sender
   {
        FootBallPlayersViewController *controller = [[FootBallPlayersViewController alloc]initWithNibName:@"FootBallPlayersViewController" bundle:nil];
        controller.delegate = self; //u must set to self
        [self presentViewController:controller animated:YES completion:nil];


   }

  - (void)didReceiveMemoryWarning
  {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
  }

  - (void)selectedFootBallPlayer:(NSString *)player
    {
      //implementation of your delegate method

      //hear u are getting the football player name and u can continue further hear
       NSLog(@"%@",player);
      if([player isEqualToString:@"player1"])
        {
          UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
          [aButton setTitle:player forState:UIControlStateNormal];
          [aButton addTarget:self action:@selector(whenFirstPlayerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //add the target to self for click events 
          aButton.frame = CGRectMake(50, 50, 200, 55);
          [self.view addSubview:aButton];

       }
     else
      {
       UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       [aButton setTitle:player forState:UIControlStateNormal];
       aButton.frame = CGRectMake(50, 105, 200, 55);
        [aButton addTarget:self action:@selector(whenSecondPlayerButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; //same hear
       [self.view addSubview:aButton];

      }


  }
   //now define the action methods 
 - (void)whenFirstPlayerButtonClicked:(UIButton *)sender
  {

       NSLog(@"player 1 video start");    
  }

  - (void)whenSecondPlayerButtonClicked:(UIButton *)sender
  {
       NSLog(@"player 2 video start ");
  }


  @end

在包含 tableview 的 View 中做这样的事情

//在FootBallPlayersViewController.h中
   #import <UIKit/UIKit.h>
     @protocol FootballPlayerDelegate <NSObject>     //define a protocol named      FootballPlayerDelegate
    - (void)selectedFootBallPlayer:(NSString *)player;
    @end

   @interface FootBallPlayersViewController : UIViewController
    {
       NSArray *players;
       NSString *selectedPlayer;
    }

   @property (retain, nonatomic) IBOutlet UITableView *playerTable;
   @property (nonatomic, assign) id<FootballPlayerDelegate>delegate; //create a delegate

   @end



在您的 FootBallPlayersViewController.m文件
   #import "FootBallPlayersViewController.h"

   @interface FootBallPlayersViewController ()<UITableViewDataSource,UITableViewDelegate>
     {


    }
   @end

   @implementation FootBallPlayersViewController
   @synthesize delegate; //synthesizing the delegate

   - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
   {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
      // Custom initialization
         }
     return self;
    }

    - (void)viewDidLoad
   {
       [super viewDidLoad];
        players = [[NSArray alloc]initWithObjects:@"player1",@"player2", nil];
      // players = [[NSArray alloc]initWithObjects:@"player1","player2", nil];
     // Do any additional setup after loading the view from its nib.
   }

    - (void)didReceiveMemoryWarning
    {
      [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
    }

   - (void)dealloc
  {
      [players release];
      [_playerTable release];
      [super dealloc];
  }
 - (IBAction)whenDoneButtonClicked:(id)sender {
      //when done button clicked -->
      //send a delegate to main controller
      if([self.delegate respondsToSelector:@selector(selectedFootBallPlayer:)])//to avoid crash
        {
          [self.delegate selectedFootBallPlayer:selectedPlayer]; //call the delegate method hear
        }
     //dismiss the view
     [self dismissViewControllerAnimated:YES completion:nil];

    }


    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
     {
        return 1;
      }

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

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {

        UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
        if(cell == nil)
       {
          cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
       }

        cell.textLabel.text = [players objectAtIndex:indexPath.row];
        return cell;
   }

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
   {
      //u can manage check mark and all, i am getting the selected player name
     selectedPlayer = [players objectAtIndex:indexPath.row];
    }

 @end

关于ios - 在表格 View 中保存选定的行并返回主屏幕 UIButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946704/

相关文章:

ios - 如何在 Swift 4.0 中使用键值编码?

iphone - 在 drawRect 中绘制对象与添加 subview 和移动它们的框架

objective-c - NSImage initWithContentsOfFile : display progress

iphone - 是否有可能在不越狱的情况下测试 iPhone 应用程序?

ios - DJI Mobile SDK如何获取遥控连接信号和相机高清信号

iphone - 加载 UIImage 低质量

ios - 为什么 Crashlytics 中缺少 CLSLog 消息?

ios - 从另一个应用程序打开 iOS 应用程序

iphone - iOS 中的本地 Assets URL 有多独特

iphone - 如何在 UILabel 中换行