0

我正在测试我的 ios 应用程序,它的部署目标为 5.0,基础 SDK 为 6.1。

在 ios 5.0 和 ios 5.1 中一切正常,但在 ios 6.0 中,我在索引处插入子视图时遇到问题。子视图是一个表格视图,父视图是一个作为特殊类“UIAlertTableView”创建的 uialertview。出现警报视图,但似乎没有插入任何内容。在此之前,我已经修复了超级视图(横向)的自动旋转问题,因为众所周知 ios 6.0 处理旋转的方式不同,所以现在我的超级视图正确显示,但正如我所说,这个警报视图弹出没有表格现在。我是否想解决 tableview 和 superview 的自动旋转问题?我不认为这是必要的,因为 tableview 没有在导入的类中声明,而是在父视图控制器中声明。

     /*UIAlertTableView.m   (the imported object class)*/
    @interface UIAlertView (private)
    - (void)layoutAnimated:(BOOL)fp8;
    @end

    @implementation UIAlertTableView

    @synthesize tableWidth;
    @synthesize tableHeight;
    @synthesize lowestView;
    @synthesize kTablePadding;
    @synthesize alertDelegate;



    - (void)layoutAnimated:(BOOL)fp8 {
        [super layoutAnimated:fp8];
        [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - tableExtHeight/2, self.frame.size.width, self.frame.size.height + tableExtHeight)];

        // We get the lowest non-control view (i.e. Labels) so we can place the table view just below

        int i = 0;
        while (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) {
            lowestView = [self.subviews objectAtIndex:i];
            i++;
        }

        tableWidth = 262.0f;



        for (UIView *sv in self.subviews) {
            // Move all Controls down
            if ([sv isKindOfClass:[UIControl class]]) {
                sv.frame = CGRectMake(sv.frame.origin.x, sv.frame.origin.y + tableExtHeight, sv.frame.size.width, sv.frame.size.height);
            }
        }

    }

    - (void)show{
        [self prepare];
        [super show];
    }

    - (void)prepare {
        if (tableHeight == 0) {
            tableHeight = 150.0f;
        }
        kTablePadding = 8.0f;

        tableExtHeight = tableHeight + 2 * kTablePadding;

        [self setNeedsLayout];
    }


    @end

    /*the UIAlertTableView class is imported into the myViewController header file*/


   /*myViewController.m*/

    @implementation myViewController
    @synthesize myTableView;
    @synthesize alert;
    @synthesize imageView;
    @synthesize scrollView;
    @synthesize models;
    @synthesize picked;





    #pragma myTableView

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

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


        return [models count];    

    }

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


        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:CellIdentifier];

        }




        // now configure the cell

        cell.textLabel.text = [models objectAtIndex:[indexPath row]];
        [cell setAccessibilityTraits: UIAccessibilityTraitButton];




        return cell;


    }
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {



        if (returnedSetting && (indexPath.row == prevSelectedIndex)){
            returnedSetting = FALSE;






        }
    }

    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        //index path to row that's selected 

        NSIndexPath *myIndexPath = [myTableView indexPathForSelectedRow];
        UITableViewCell *cell = [myTableView cellForRowAtIndexPath:myIndexPath];
        labelText = cell.textLabel.text;
        selectedModel = cell.textLabel.text;


    }
    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        NSIndexPath *myIndexPath = [tableView indexPathForSelectedRow];
        prevSelectedIndex = myIndexPath.row;
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:myIndexPath];
        [alert dismissWithClickedButtonIndex:0 animated:YES];
        labelText = cell.textLabel.text;
        selectedModel = cell.textLabel.text;


        [self dismissModalViewControllerAnimated:YES];


    }



    -(void)removeButton{
        [UIView animateWithDuration:1.5
                              delay:1.5  
                            options:UIViewAnimationCurveEaseInOut 
                         animations:^ {
                             eButton.alpha = 0;

                         } 
                         completion:^(BOOL finished) {


                         }];
    }




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

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }
    -(void)showAlertFor:(NSTimer *)timer{



        [alert show];
        myTableView.frame = CGRectMake(11.0f, alert.lowestView.frame.origin.y + alert.lowestView.frame.size.height + 2 * alert.kTablePadding, alert.tableWidth, alert.tableHeight);
        [alert insertSubview:myTableView atIndex:1];
         [myTableView performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:.3];


    }
    -(void)bringupAlertTableViewFor:(NSString *)dName atLocationOnScreen:(CGPoint)newPoint{

        if (([dName isEqualToString:@"hello"]){

            picked =TRUE;



            myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStylePlain];



            alert = [[UIAlertTableView alloc] initWithTitle:dName
                                                    message:@"Choose from the table below:"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:nil];
            [alert setDelegate: alert.alertDelegate];

            models = [[NSMutableArray alloc]init];
            myTableView.delegate = self;
            myTableView.dataSource = self;

            NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Decisions" inManagedObjectContext:context];
            NSFetchRequest *request = [[NSFetchRequest alloc]init];
            [request setEntity:entitydesc];



            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dName == %@", dName];
            [request setPredicate: predicate];


            NSError *error;

            //matches found
            NSArray *matchingData = [context executeFetchRequest: request error: &error];



            for (NSManagedObject *obj in matchingData) {
                [models addObject:[NSString stringWithFormat:@"%@",[obj valueForKey: @"model"]]];


            }


            alert.tableHeight = 120;








            [alert show];


            myTableView.frame = CGRectMake(11.0f, alert.lowestView.frame.origin.y + alert.lowestView.frame.size.height + 2 * alert.kTablePadding, alert.tableWidth, alert.tableHeight);


            [alert insertSubview:myTableView atIndex:1];



            [myTableView performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:.3];




        }else{

            picked = TRUE;

            frame.origin.x = newPoint.x - 29; // new x coordinate
            frame.origin.y = 240; // new y coordinate

            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration: 1.5];

            [UIView commitAnimations];



            myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStylePlain];

            alert = [[UIAlertTableView alloc] initWithTitle:dName
                                                    message:@"Select a choice:"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:nil];
            [alert setDelegate: alert.alertDelegate];

            models = [[NSMutableArray alloc]init];
            myTableView.delegate = self;
            myTableView.dataSource = self;

            NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"Decisions" inManagedObjectContext:context];
            NSFetchRequest *request = [[NSFetchRequest alloc]init];
            [request setEntity:entitydesc];



            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deName == %@", dName];
            [request setPredicate: predicate];


            NSError *error;

            //matches found
            NSArray *matchingData = [context executeFetchRequest: request error: &error];



            for (NSManagedObject *obj in matchingData) {
                [models addObject:[NSString stringWithFormat:@"%@",[obj valueForKey: @"model"]]];


            }


            alert.tableHeight = 120;





                           [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(showAlertFor:) userInfo:nil repeats:NO];





            previousPoint = newPoint;




            } 







    }




    -(IBAction)singleTapImageView:(UITapGestureRecognizer *)sender {

        CGPoint pt = [sender locationInView: sender.view];




    //find out which was pressed




        if (  ((pt.x >= 52) && (pt.x <= 79)) && ((pt.y >= 269) && (pt.y <= 296))){
                   CGPoint newPoint = {45, (257 + 55)};

            [self bringupAlertTableViewFor:@"Choice1" atLocationOnScreen:newPoint];



        }


    }








    #pragma mark - View lifecycle




    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

    -(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)sView{

     [self removeButton];
    }


    -(void)scrollViewDidScroll:(UIScrollView *)sView{

        eButton.alpha = .7;


    }

    -(void)scrollViewDidEndDecelerating:(UIScrollView *)sView
    {
        [self removeButton];



    }

    -(void)exitButtonPressed{
        [self dismissModalViewControllerAnimated:YES];
    }




    - (void)viewDidLoad
    {   


        UIImage *imageS = [UIImage imageNamed:@"ti.png"];

     imageView = [[TouchDetectingImageView alloc]initWithImage:imageS];
           [imageView setDelegate:self];

        imageView.frame = CGRectMake(20, 25, imageS.size.width,imageS.size.height);

        CGFloat newScrollWidth = imageView.image.size.width + 20;

        [scrollView setContentSize:(CGSizeMake(newScrollWidth, imageView.image.size.height))];


        [scrollView addSubview: imageView];


         imageView.contentMode = UIViewContentModeScaleToFill;
          UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapImageView:)];
        singleTap.numberOfTapsRequired = 1;
          [scrollView addGestureRecognizer:singleTap]; 




        UIImage *img = [UIImage imageNamed:@"backbutton.png"];
    CGRect frameForButton = CGRectMake(0, 3, img.size.width, img.size.height);
        eButton = [[exitButton alloc] initWithFrame:frameForButton];
        [eButton setDelegate:self];
        [eButton addTarget:self action:@selector(exitButtonPressed) forControlEvents:UIControlEventTouchUpInside];


        UITapGestureRecognizer *buttonTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(exitButtonPressed)];

        buttonTap.numberOfTapsRequired = 1;
        [eButton addGestureRecognizer:buttonTap];

        eButton.alpha = 0;


        [self.view addSubview:eButton];

            [super viewDidLoad];



       }

    - (void)viewDidAppear:(BOOL)animated
    {








        [super viewDidAppear:animated];



    }


    - (void)viewDidUnload
    {
        [self setScrollView:nil];

        [self setImageView:nil];



        [self setmyTableView:nil];
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
        [scrollView flashScrollIndicators];
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // The menu is only going to support landscape orientations


        return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
    }
    - (NSInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
    }

    -(BOOL)shouldAutorotate{
        return YES;
    }

    @end
4

1 回答 1

0

这样做的原因是:

1) 对 layoutAnimated 的调用被忽略

2) ios 6 处理视图层次结构的方式似乎有所不同......

3) ios 6.0 会自动缩放以适应 1136x640 显示器,而早期版本会自动缩放以适应 960x640 显示器。

解决方案是:

  • 使用 layoutsubviews 调用和 layoutifneeded

  • 还使用条件语句来查找 ios 版本(例如,greater_than_or_equal_to ios 6.0)

于 2013-04-27T18:52:03.520 回答