如何在按钮点击时加载UIView

3

我有一个单独的UIView,我在UIViewController中使用它,它最初会加载,但是当我再次点击按钮时,我希望它重新加载,因为它有一个绘制图形的方法,需要在视图加载时绘制图形。

  #import <UIKit/UIKit.h> 
  #import "ECGraph.h"
  #import "ECGraphItem.h"
  #import "CereniaAppDelegate.h"
  @class GraphsViewController;
  @interface Display : UIView {

NSArray *percentages;
CereniaAppDelegate*appDelegate;
}
  @property(nonatomic,retain)NSArray*percentages;

  -(void) setPercentageArray:(NSArray*) array;
  @end

实现文件

 #import "Display.h"
 #import "ECGraph.h"
 #import "CereniaAppDelegate.h"
 @implementation Display
 @synthesize percentages;
 - (id)initWithFrame:(CGRect)frame {
 self = [super initWithFrame:frame];
 if (self) {
  }
 return self;
 }

- (void)drawRect:(CGRect)rect {

 CGContextRef _context = UIGraphicsGetCurrentContext();


 ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(70,-70,800,200) withContext:
                  _context isPortrait:NO];

    appDelegate=[[UIApplication sharedApplication]delegate];


ECGraphItem *item1 = [[ECGraphItem alloc] init];

ECGraphItem *item2 = [[ECGraphItem alloc] init];    

ECGraphItem *item3 = [[ECGraphItem alloc] init];
    item1.isPercentage = YES;

int value=(int)roundf(appDelegate.graphValueOne);
item1.yValue=value;
item1.width = 70;
item1.name = @"Unvaccinated horse"; 

int value1=(int)roundf(appDelegate.graphValueTwo);

item2.isPercentage = YES;
item2.yValue=value1;
item2.width = 70; 

item2.name = @"Annually Vaccinated horse";

int value3=(int)roundf(appDelegate.graphValueThree);
item3.isPercentage = YES;
item3.yValue=value3;

item3.width = 70;
item3.name = @"Semi-Annually vaccinated horse";






NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,item3,nil];
[graph setXaxisTitle:@""];
[graph setYaxisTitle:@"Risk"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor lightGrayColor]];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];


    }
  -(void) setPercentageArray:(NSArray*) array


    {


percentages = array;


NSString*test=[percentages objectAtIndex:0];

NSLog(test);


     }



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

   @end

先隐藏 UView,然后再取消隐藏,或者重新分配它。 - IronManGill
@Gill,仅添加和删除UIView并不会重新加载其内容。您必须重新分配并再次添加它。 - Girish
@Girish,是的,你说得对。你能告诉我如何重新分配并在加载后添加它吗? - user1808433
@Girish 我在某个地方写了allocate,请再读一遍 :) - IronManGill
在.h文件中创建该视图的对象并检查其是否存在,如果存在则释放,否则分配新对象并添加它。这可能会解决您的问题。 - Girish
1个回答

6
您可以使用以下方法使任何视图重新绘制:
[view setNeedsDisplay];

所以在刷新按钮上,您需要更新Display类使用的数据,然后设置setNeedsDisplay。


在更新数据后调用此方法的位置。 - user1808433
你的视图控制器中可能会有一个与重新加载按钮链接的IBAction。把它放在那里即可。 - Cocoanetics
@interface RiskViewController : UIViewController {Display *display;这是视图控制器,我在这里使用该视图,所以如何重新绘制它。 - user1808433
是的,我已经把它放在那里了,但它没有再次创建视图。 - user1808433
在更新显示数据后:[display setNeedsDisplay] - Cocoanetics

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接