UITableViewController as a subview getting error - ios

I have a UIViewController which is being loaded onto my UIViewController as a subview, however I am getting this error
Property 'frame' not found on object of type 'mynamedTableViewController *'
This is what my code looks like for the UIViewController.
.h
#import <UIKit/UIKit.h>
#import "FilterButtonsTableViewController.h"
#interface MainFilterViewController : UIViewController
#property (strong, nonatomic) UIButton *applyButton;
#property (strong, nonatomic) UIButton *cancelButton;
#property (strong, nonatomic) UIButton *clearAllButton;
#property (strong, nonatomic) FilterButtonsTableViewController *filterButtonsTableViewController;
#end
.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// add header label
UILabel *noteHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(25.0, 0.0, 200.0, 45.0)];
noteHeaderLabel.backgroundColor = [UIColor clearColor];
noteHeaderLabel.textColor = [UIColor lightGrayColor];
// dynamic using whatViewName
noteHeaderLabel.text = #"Filter";
[self.view addSubview:noteHeaderLabel];
// add underlines
// top
UIView *topUnderline = [[UIView alloc] initWithFrame:CGRectMake(0.0, 45.0, self.view.frame.size.width, 1.0)];
topUnderline.backgroundColor = [UIColor lightGrayColor];
topUnderline.alpha = 0.8;
// bottom
UIView *bottomUnderline = [[UIView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height-45.0, self.view.frame.size.width, 1.0)];
bottomUnderline.backgroundColor = [UIColor lightGrayColor];
bottomUnderline.alpha = 0.8;
// addviews
[self.view addSubview:topUnderline];
[self.view addSubview:bottomUnderline];
// add buttons to view
// apply
applyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
applyButton.userInteractionEnabled = YES;
[applyButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchDown];
[applyButton setTitle:#"Save" forState:UIControlStateNormal];
applyButton.frame = CGRectMake(self.view.frame.size.width - 130, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:applyButton];
// cancel
cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cancelButton.userInteractionEnabled = YES;
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
cancelButton.frame = CGRectMake(self.view.frame.size.width - 260, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:cancelButton];
// cancel
clearAllButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
clearAllButton.userInteractionEnabled = YES;
[clearAllButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[clearAllButton addTarget:self action:#selector(alertViewSelected:) forControlEvents:UIControlEventTouchUpInside];
[clearAllButton setTitle:#"Clear filter" forState:UIControlStateNormal];
clearAllButton.frame = CGRectMake(0.0, self.view.frame.size.height - 43, 120.0, 40.0);
[self.view addSubview:clearAllButton];
//TavleView
filterButtonsTableViewController = [[[FilterButtonsTableViewController alloc] init];
filterButtonsTableViewController.frame:CGRectMake(5.0, 65.0, (self.view.frame.size.width/2)-10, 235.0);
[self.view addSubview:filterButtonsTableViewController.view];
}

A viewcontroller does not have a frame property, only its view does.
Try this instead :
filterButtonsTableViewController.view.frame = CGRectMake(5.0, 65.0, (self.view.frame.size.width/2)-10, 235.0);

#interface MainFilterViewController : UIViewController
filterButtonsTableViewController = [[FilterButtonsTableViewController alloc] init];
[self.view addSubview:filterButtonsTableViewController.view];
// this code helps you to add tableview controller as a sub view.
#interface FilterButtonsTableViewController
self.view.frame=CGRectMake(0, 50, 150, 150);
// frame of the tableviewController is set on his class.

Related

Cannot get UIButton to programmatically call action in iOS

I cannot get my button to fire the associated method. I am not using IB or anything like that (all programmatic). The _mainViewController is passed in during custom init, just to clarify (although this should have nothing to do with the button). Please see my code snips:
viewcontroller.h
#import <UIKit/UIKit.h>
#interface CSNWZSSViewController : UIViewController
- (void)doneButtonPressed;
#property UIViewController *mainViewController;
#property UIButton *doneButton;
#end
viewcontroller.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _mainViewController.view.frame.size.width, 45)];
headerView.backgroundColor = [UIColor darkGrayColor];
_doneButton = [[UIButton alloc] initWithFrame:CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height)];
[_doneButton addTarget:self
action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:#"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
[headerView addSubview:_doneButton];
CGRect headerLabelFrame = CGRectMake(5, 5, headerView.frame.size.width - 105, headerView.frame.size.height - 5);
UILabel *headerLabel = [[UILabel alloc] initWithFrame:headerLabelFrame];
headerLabel.text = #"test header";
headerLabel.textColor = [UIColor whiteColor];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.font = [UIFont systemFontOfSize:20.0f];
headerLabel.lineBreakMode = NSLineBreakByTruncatingTail;
[headerView addSubview:headerLabel];
[self.view addSubview:headerView];
}
- (void)doneButtonPressed
{
NSLog(#"button pressed");
}
Update 10/9/16 using buttonWithType init and explicit userInteractionEnabled
_doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_doneButton addTarget:self
action:#selector(doneButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[_doneButton setTitle:#"Done" forState:UIControlStateNormal];
_doneButton.tintColor = [UIColor whiteColor];
_doneButton.backgroundColor = [UIColor blackColor]; //[UIColor colorWithRed:0 green:.77 blue:0 alpha:1];
_doneButton.frame = CGRectMake(_mainViewController.view.frame.size.width - 100, 0, 100, headerView.frame.size.height);
_doneButton.userInteractionEnabled = YES;
[headerView addSubview:_doneButton];
Note 12/9/16 the _mainViewController object is passed in here:
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
Solution with matt's help (see below) The context was getting messed up due to the lack of adding the new view controller.
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController addChildViewController:CSNWZSSVC];
[self.viewController.view addSubview:CSNWZSSVC.view];
The _mainViewController is passed in during custom init
I'm going to guess that this is the problem — that you are mismanaging this view controller and that it is not being correctly made part of the view controller hierarchy, and in fact may even be going out of existence so that there is no one to send the button action message to.
edit Yes, now that you've posted more code, I'm clearly right:
CSNWZSSViewController *CSNWZSSVC = [[CSNWZSSViewController alloc] initWithRichTextString:source withTitleString:title forViewController:self.viewController];
[self.viewController.view addSubview:CSNWZSSVC.view];
That is totally wrong behavior. You cannot arbitrarily add another view controller's view to yours. There is a strict dance for managing a child view controller, and you are not doing the dance.
Try:
UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
And then set your frame.

When a custom button is pressed UILabel won't update (Objective-C)

(I'm currently learning iOS Development so I apologize in advanced of my limited knowledge.)
What I am trying to accomplish is that whenever you press either the addition or subtraction button the label will increment by one and will display the result by the label. I am having difficulty with displaying the result in the label.
This is my ViewController.h file:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController{
UILabel *valueLabel;
}
#end
This is my ViewController.m file:
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
int w;
w = 0;
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
// AddButton
UIButton *additionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
additionButton.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:additionButton];
// Subtraction button
UIButton *subtractionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
subtractionButton.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:subtractionButton];
// Label
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:valueLabel];
}
#end
This is my Delegate and Protocol:
StepperView.h
#import <UIKit/UIKit.h>
#protocol SwitchViewDelegate
- (void)switchViewValueChanged:(int)number;
#end
#interface StepperView : UIView
{
}
#property (nonatomic,weak) id<SwitchViewDelegate> delegate;
#end
This is my StepperView.m file
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftView];
// Label
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(stepperView.frame.size.width/2, stepperView.frame.size.height/2, 100, 100)];
[valueLabel setText:#"10"];
[valueLabel setTextColor:[UIColor redColor]];
[self addSubview:valueLabel];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
NSLog(#"MINUS");
} else {
NSLog(#"PLUS");
}
// NSLog(#"%#",title);
}
#end
I am not sure if i understand clear what you want to do, but why don't you try to use the UIStepper button provided by iOS?
You can add an IBAction to your viewController and change the label with the value from the UIStepper.
Using swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
var currentValue = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func touchStepper(sender: UIStepper) {
self.valueLabel.text = "\(sender.value)"
}
}
It's not very clear what you're trying you obtain as UI, i'm posting a possible solution which is using
The delegate to pass the information from StepperView to the ViewController
UILabel to present the result which is subview of ViewController's view, instead to be subview of StepperView
ViewController.h:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) UILabel *valueLabel;
#end
ViewController.m
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController () <StepperViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
stepperView.delegate = self;
// Label
self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[self.valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:self.
valueLabel];
}
#pragma mark - StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value {
self.valueLabel.text = [NSString stringWithFormat:#"%d", value];
}
#end
StepperView.h
#import <UIKit/UIKit.h>
#class StepperView
#protocol StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value
#end
#interface StepperView : UIView
#property (nonatomic, weak) id<StepperViewDelegate> delegate;
#property (nonatomic, assign) NSInteger stepperValue;
#end
StepperView.m
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
self.stepperValue--;
} else {
self.stepperValue++;
}
[self.delegate stepperView:self valueChanged:self.stepperValue];
// NSLog(#"%#",title);
}
#end
This should give you more clear vision how different components should corespondent with each other.

How to remove sub views from parent view

Hi i am beginner in ios and i am trying to add multiple child views programmatically on my parent class they are adding fine(here i am loading child views from background class)
But when i add second child view on my parent view class i want to remove first child view on my parent view class but it is not removing my code is below please help me some one
My code:-
child class:-
#import "MainView1.h"
#interface MainView1 ()
#end
#implementation MainView1
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loadView1 :(UIView *)myview :(int)viewValue :(UIViewController*)VC
{
UIView * firstview;
UIView * secondview;
if (viewValue == 1) {
[firstview willRemoveSubview:myview];
[secondview willRemoveSubview:myview];
firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[firstview setBackgroundColor:[UIColor yellowColor]];
UIButton *addProject=[[UIButton alloc]init];
addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(100, 285, 100, 18);
addProject.backgroundColor = [UIColor redColor];
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:VC action:#selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[firstview addSubview:addProject];
[myview addSubview:firstview];
}
else {
[firstview willRemoveSubview:myview];
[secondview willRemoveSubview:myview];
secondview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 400)];
[secondview setBackgroundColor:[UIColor greenColor]];
UIButton *addProject1=[[UIButton alloc]init];
addProject1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject1.frame = CGRectMake(200, 285, 100, 18);
addProject1.backgroundColor = [UIColor redColor];
[addProject1 setTitle:#"Show View1" forState:UIControlStateNormal];
[addProject1 addTarget:VC action:#selector(ProjectPressed123:) forControlEvents:UIControlEventTouchUpInside];
[secondview addSubview:addProject1];
[myview addSubview:secondview];
}
}
parent class:-
when i clicked on below ProjectPressed button action i want to add second child view on my parent view class, It's adding fine but first child view is not removed from parent view class
#import "ViewController.h"
#import "MainView1.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MainView1 * m1 = [[MainView1 alloc]init];
[m1 loadView1:self.view :1 :self];
}
- (void)ProjectPressed:(id)sender
{
MainView1 * m1 = [[MainView1 alloc]init];
[m1 loadView1:self.view :2 :self];
}
Try this
[firstview removeFromSuperview]
[secondview removeFromSuperview]
Please try now
-(void)loadView1 :(UIView *)myview :(int)viewValue :(UIViewController*)VC
{
UIView * firstview;
UIView * secondview;
for (UIView *view in [myview subviews])
{
[view removeFromSuperview];
}
if (viewValue == 1) {
firstview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
[firstview setBackgroundColor:[UIColor yellowColor]];
UIButton *addProject=[[UIButton alloc]init];
addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(100, 285, 100, 18);
addProject.backgroundColor = [UIColor redColor];
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:VC action:#selector(ProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[firstview addSubview:addProject];
[myview addSubview:firstview];
}
else {
secondview =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 400)];
[secondview setBackgroundColor:[UIColor greenColor]];
UIButton *addProject1=[[UIButton alloc]init];
addProject1 = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject1.frame = CGRectMake(200, 285, 100, 18);
addProject1.backgroundColor = [UIColor redColor];
[addProject1 setTitle:#"Show View1" forState:UIControlStateNormal];
[addProject1 addTarget:VC action:#selector(ProjectPressed123:) forControlEvents:UIControlEventTouchUpInside];
[secondview addSubview:addProject1];
[myview addSubview:secondview];
}
}

UIButton not changing anything with #selector - new Issue

Need help. I am trying to change an image of an ImageView using a programmed button. I copied some of the sample codes out here. The sitting on highlighted button works but not doing anything else when clicked. Thanks.
Thanks for the replies. I managed to change the photos but created more problem. I believe it is clear that that I am new to Xcode.
New problems:
1. The new image stays on top so i cannot access the button.
2. I would like to have the image changed only when the button is highlighted.
3. With the new image, it created warning: "Hides instance variable" on the relayImages properties. Thanks again.
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
#synthesize relayImages;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self addMyImages];
[self addMyButton];
[self addMyLabel];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)addMyButton
{
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 20, 78)];
UIImage *offState = [UIImage imageNamed:#"PBNoOff.png"];
[playButton setBackgroundImage:offState forState:UIControlStateNormal];
UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[playButton setBackgroundImage:onState forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"1_2RelaysOn.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
[self.view addSubview:_playButton];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"1_2RelaysOff.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyLabel
{
UILabel *checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 30)];
checkStatusLabel.backgroundColor = [UIColor greenColor];
checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:checkStatusLabel];
}
try this i am created custom imageView
.m file
-(void)addMyButton
{
_playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 60, 50)];
[_playButton setTitle:#"button" forState:UIControlStateNormal];
[_playButton setBackgroundColor:[UIColor grayColor]];
UIImage *offState = [UIImage imageNamed:#"1.jpg"];
// [playButton setBackgroundImage:offState forState:UIControlStateNormal];
// UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[_playButton setBackgroundImage:offState forState:UIControlStateHighlighted];
[_playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"2.jpg"];
relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(20, 100, 100, 100);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"3.jpg"];
UIImageView *someImages = [[UIImageView alloc] initWithImage:image];
someImages.frame = CGRectMake(20, 200, 100, 100);
someImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:someImages];
}
-(void)addMyLabel
{
_checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100, 30)];
_checkStatusLabel.backgroundColor = [UIColor greenColor];
_checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:_checkStatusLabel];
}
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
and viewdidload
[self addMyButton];
[self addMyImages];
[self addMyLabel];

How to dismiss a subView generated by code?

In my project I want to show a first run view when starting the app for te first time. To do this I added a UIView to my mainViewCntroller.m. To dismiss this (overlayed) view I put a button on the view called acceptButton. What code do I have to add to the Button to remove this view from the stack?
What do I do wrong?
Here's my code:
- (void) firstRun {
if (((AppDelegate*)[UIApplication sharedApplication].delegate).firstRun)
{
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
NSLog(#"%f", height);
CGRect myFrame = CGRectMake(0, 0, width, height);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
// Create a ScrollView and a label
UIScrollView *discScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(20.0f, 0.0f, self.view.frame.size.width - 20, self.view.frame.size.height -70)];
discScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UILabel *discLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
discLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *labelText = NSLocalizedString (#"InfoText",#"");
[discLabel setText:labelText];
// Tell the label to use an unlimited number of lines
[discLabel setNumberOfLines:0];
[discLabel setNumberOfLines:0];
[discLabel setBackgroundColor:[UIColor clearColor]];
[discLabel setFont:[UIFont boldSystemFontOfSize:17]];
discLabel.textColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0];
//[infoLabel sizeToFit];
CGSize infoLabelSize = [discLabel.text sizeWithFont:discLabel.font
constrainedToSize:CGSizeMake(discScroll.frame.size.width, 5000)
lineBreakMode:UILineBreakModeWordWrap];
discLabel.frame = CGRectMake(0, 0, infoLabelSize.width, infoLabelSize.height);
discScroll.contentSize = infoLabelSize;
[discScroll addSubview:discLabel];
[self.view addSubview:discScroll];
UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
acceptButton.frame = CGRectMake(110, [[UIScreen mainScreen] bounds].size.height - 74, 100, 44);
// position in the parent view and set the size of the button
[acceptButton setTitle:#"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:acceptButton];
}
}
-(IBAction)buttonClicked:(id)sender
{
[[self.myView viewWithTag:12345] removeFromSuperview];
}
In my mainViewController.h is the following property:
#property (weak, nonatomic) IBOutlet UIView *myView;
EDIT I've post the complete code. Maybe something else is in the way.
Ive written it in Xcode, following Code works for me.
Ive opened a new XCode Project (Single View based), and just added following Code:
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
UIButton *acceptButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
acceptButton.frame = CGRectMake(0, 0, 100, 44);
[acceptButton setTitle:#"Click Me!" forState:UIControlStateNormal];
[acceptButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[myView addSubview:acceptButton];
[myView release];
}
- (void)buttonClicked{
[[self.view viewWithTag:12345] removeFromSuperview];
}
This worked for me in iPhone Simulator.... Just this code in the MainViewController.m nothing else... Just give it a try, hope it works for you too :-)
[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
when your method is called acceptButton, change to
[acceptButton addTarget:self action:#selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];
your target is set to buttonClicked but you are using acceptButton
[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)buttonClicked:(id)sender
{
[self.myView removeFromSuperview];
}
The problem lies with the [acceptButton addTarget:self action:#selector(buttonClicked:) line. Change buttonClicked to your method acceptButton: in that call.
[acceptButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
The exception is clear, there is no function named "buttonClicked"
Just change the part of your code with below code;
[acceptButton addTarget:self action:#selector(acceptButton:) forControlEvents:UIControlEventTouchUpInside];
just give it a tag...
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
myView.tag = 12345;
[self.view addSubview:myView];
[myView release];
-(IBAction)acceptButton:(id)sender{
[[self.view viewWithTag:12345] removeFromSuperview];
}
Also change
action:#selector(buttonClicked:)
to
action:#selector(acceptButton:)

Resources