ios - 使用 UIButton 从其他 UIViewController 中删除一行

标签 ios objective-c uitableview uiviewcontroller uibutton

我有一个问题,我有一个UITableViewController,它有一个列表,当我按下一行时,它会将我发送到另一个UIViewController

我用 prepareForSegue 方法实现了这个,我也放了

- (void)tableView:(UITableView *)tableView commitEditingStyle:

当我滑动一行时正在删除。一切都很完美。

现在,我的问题是,我在 UIViewController 中放置了一个 UIButton,我希望在按下它时从 UITableViewController 中删除该行,该怎么做?

最佳答案

您可以使用委托(delegate),因为您希望在两个 View Controller 之间进行通信。

在 DetailViewController 中创建协议(protocol)。当您最初从 TableViewController 转到 DetailViewController 时,将“idx”设置为选定的 indexPath.row 或数组索引。

当您从 DetailViewController 中删除它时,委托(delegate)会将索引发送到 TableViewController,您可以在那里从主数组中删除它。

DetailViewController.h文件

    #import <UIKit/UIKit.h>


  @protocol DetailViewControllerDelegate <NSObject>

  @optional
  -(void) removeElementAt:(int )index;
  @end

  @interface DetailViewController : UIViewController{

        IBOutlet UIButton *bttn;
        id <DetailViewControllerDelegate> delegate;

    }

    @property (retain) int idx;

    @property (retain) id delegate;

    -(IBAction)bttnclicked;
    -(IBAction)back:(id)sender;

    @end

在DetailViewController.m文件中

 #import "DetailViewController.h"
    #import "TAbleViewController.h"

    @interface DetailViewController ()

    @end

    @implementation DetailViewController

    @synthesize idx,delegate;

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



          - (void)viewDidLoad
            {
                [super viewDidLoad];
                // Do any additional setup after loading the view from its nib.
            }

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

            -(IBAction)bttnclicked{
               [[self delegate] removeElementAt:idx];
            }

            -(IBAction)back:(id)sender{
                [self dismissViewControllerAnimated:YES completion:NULL];
            }

            @end

对于TableViewContoller.h文件

 #import <UIKit/UIKit.h>
   #import "DetailViewController.h"

    @interface TableViewContoller : UIViewController <DetailViewControllerDelegate>
    {
        DetailViewController *secondview;

    }

    @end

在TableViewController.m文件中,

   #import "TableViewController.h"
   #import "DetailViewController.h"

   @interface TableViewController ()

                @end

                @implementation TableViewController

                - (void)viewDidLoad
                {
                    [super viewDidLoad];
                    // Do any additional setup after loading the view, typically from a nib.
                }

                - (void)didReceiveMemoryWarning
                {
                    [super didReceiveMemoryWarning];
                    // Dispose of any resources that can be recreated.
                }
                  -(void) removeElementAt:(int )index
            {
                NSLog(@"Before object : %@",self.objects);

                [self.objects removeObjectAtIndex:index];
                //reload table at your convinience
                NSLog(@"Removed object : %@",self.objects);

                [self.tableView reloadData];
            }


       - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"showDetail"]) {
            NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
            DetailViewController *dc=[segue destinationViewController];
            dc.idx = (int) indexPath.row;
            dc.delegate = self;

        }
    }

关于ios - 使用 UIButton 从其他 UIViewController 中删除一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214902/

相关文章:

ios - 像 Chrome ios 一样复制持久/快速滚动

ios - CLBeaconRegion,如何关闭警告 : Turn On Bluetooth to Allow * to Connect to Accessories

iphone - Target Membership 变灰(禁用)

objective-c - UITableView 处于编辑模式时可选择的 UITableViewCells

objective-c - IOS - 如何将选项卡栏添加到导航 Controller (主从模板)?

ios - 如何在异步获取 JSON 数据以填充到 UITableView 时显示 UIActivityIndi​​catorView?

ios - 使用 OCMock 模拟类选择器

ios - 当我转到 Debug模式时,Pods 中的 .a 文件变为红色

ios - refreshControl 扩展在 imagePicker 之后不显示微调器

ios - 在 TableView 中显示 plist 中的数据数组