在表视图中保存选定的行并返回主屏幕UIButtons

时间:2013-11-13 06:16:06

标签: ios iphone objective-c ios4 xcode4.2

Main Screen

Table View select specific Row and save that row

Show specific row contents on the main screen

Plz代码帮助谁能告诉我如何完成这项任务? 在主屏幕中用户选择足球运动员,在表格视图中的第二个屏幕中,用户选择特定的行并保存该行并返回主视图。在主视图中,它显示特定的行视频。 基本上我想了解特定的行选择,在表格视图中保存该选择并在主屏幕中显示他们的上下文。

4 个答案:

答案 0 :(得分:1)

通过下面的代码,它实现了委托的概念,并为你的问题实现了解决方案希望这有助于你:)


  //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的视图中做这样的事情


       //在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


答案 1 :(得分:0)

简单的解决方案...... 因为你是新手,我正在澄清每一点。

  1. 首先在AppDelegate.h中创建一个属性

    @property int selectedRow;

  2. 将选定的indexpath.row保存在表视图屏幕的第二个屏幕中,并导入AppDelegate.h

    (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
       {   
         self.appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
         self.appDelegate.selectedRow=indexPath.row; //saving the row
    
        }
    
  3. 在主屏幕的viewWillAppear()

    -(void)viewWillAppear:(BOOL)animated
      {
         if(self.appDelegate.selectedRow!=-1)//check wether row is selected or not
             {
    
                 //action to show the specific row videos
    
              }
       }
    

答案 2 :(得分:0)

实现这一目标的好方法:

  1. 自定义代表
  2. NSNotificationCenter
  3. NSUserDefaults编辑:不必要的磁盘写入
  4. 维护一个公共NSObject子类并刷新-willAppear
  5. 上的数据

    其他方式:

    1. 数据库(Core Data / SQLite)或plist(对于您的情况来说太重了)(编辑:不必要的磁盘写入
    2. UIPasteBoard

    3. 快速代表教程:


      第1部分:创建代理

      假设这位于UITableViewController子类的.h,我已命名为YourTableViewControllerClassName

      //declare the protocol
      @class YourTableViewControllerClassName;
      @protocol YourTableViewControllerClassNameDelegate <NSObject>
      //@required    //uncomment to specify required delegate methods as below
      //- (void)requiredMethodNotUsedForThisExample;
      @optional
      - (void)selectedRow: (NSString *)selectedObj;
      @end
      
      @interface YourTableViewControllerClassName : UITableViewController
      //declare a weak property to store any object
      @property (nonatomic, weak) id <YourTableViewControllerClassNameDelegate> delegate;
      @end
      

      假设这是相应-didSelectRowAtIndexPath子类的UITableViewController

      -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      
          //the following line is the main thing and can be called
          //in any method within this class (placed wisely)
          if([[self delegate] respondsToSelector:@selector(selectedRow)]) { //avoid crash
              [[self delegate] selectedRow:cell.textLabel.text];    
          }
          [self.navigationController popViewControllerAnimated:YES];
      }
      

      第2部分:实施代理

      假设这是前UIViewController子类中的代码:

      //call this method somewhere
      -(void)pushMyTableViewController
      {
          //declare "UILabel lblText;" in the .h of this class
          //lblText = [UILabel alloc] init];
          //[lblText setFrame: CGRectMake(0,0,100,35)];
          //[self.view addSubview:lblText];
      
          YourTableViewControllerClassName *tvcObj = [[YourTableViewControllerClassName alloc] init];
          //for the following line, remember to declare
          //<YourTableViewControllerClassNameDelegate> in the .h of this class
          //hence declaring that this class conforms to the delegate protocol
          [tvcObj setDelegate:self];
          [self.navigationController pushViewController:tvcObj animated:YES];
      }
      

      这将是您可以在前UIViewController子类中实现的委托方法:

      #pragma mark - Optional YourTableViewControllerClassName Delegate Methods
      -(void)selectedRow:(NSString *)selectedObj
      {
          [lblText setText:selectedObj];
      }
      

      注意:这不会解决您的特定问题,因为我们只根据UITableViewController子类中选定的行设置标签。
      关键是要说明代表团的工作方式 另外,如果您可以获取cell.textLabel.text并将其设置为前一课程中的UILabel,那么您可以在适当的位置(主要是@protocol内的方法/ s)进行更改并通过相反所选项目的数组索引或任何使您的生活更轻松的对象/变量/

      *如果您想要更轻松的话,请转到NSNotificationCenterNSUserDefaults或甚至UIPasteBoard(如果它漂浮在您的船上)

答案 3 :(得分:-1)

使用选择任何行时调用的tableView委托

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
   appDelegate.selectedindex = indexpath.row;

   or

   [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:indexpath.row] forKey:@"SelcetedIndex"];
}

然后你可以做三件事来获得你选择的索引

1)为索引路径创建一个app委托变量,以便您可以在此设置并获取其他控制器上的值

//在appDelegate文件中添加属性     @property int selectedIndex;

2)使用NSUserDefault设置所选索引值

// read userDefault value 
[[[NSUserDefaults standardUserDefaults] objectForKey:@"SelcetedIndex"] intValue];

3)使用委托将值返回给前一个控制器

//尝试google并首先了解这个概念,如果你想使用delgate,请告诉我