I have a Detail View Controller which contains data passed from a Table View:
#import "BRProjectsDetailViewController.h"
#interface BRProjectsDetailViewController ()
#end
#implementation BRProjectsDetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (IBAction)unwindBackToProjectsDetailViewController:(UIStoryboardSegue *)seque;
{
NSLog(#"unwindBackToHomeViewController!");
}
///used to feed data from homeview into detail view
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"viewWillAppear");
NSString *name = self.projects[#"name"];
self.title = name;
NSString *description = self.projects[#"description"];
self.descriptionView.text = description;
}
I want to take the variable string labelled 'name' above and pass the contents of this to a collection view controller, currently coded below. At the moment, before doing conditioning I'm just trying to get a response from my NSLog. Currently it just returns null: . This is just an excerpt from the code: Can anyone help please?
#import "BRCollectionViewController.h"
#import "BRCollectionViewCell.h"
#import "BRProjectsDetailViewController.h"
#interface BRCollectionViewController ()
{
///add arrays for photos - to update once photos known
NSArray *imagesOne;
NSArray *imagesTwo;
}
#end
#implementation BRCollectionViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
//added this
NSString *name;
NSLog(#"%#", name);
//
}
#end
Related
Suppose, I've two view controller. ViewController1 is the sub class of ViewController2. From subclass (ViewController1), I called superclass init method & passed a value by parameter & assigned that value to the superclass variable. But In superclass (ViewController2)'s viewDidLoad method, that value is always null. Why ? How can I pass that value from sub to super so that I can get that value in super class's viewDidLoad method ?
ViewController1.h
#interface ViewController : ViewController2
#end
ViewController1.m
#implementation ViewController
- (instancetype)init
{
self = [super initWithName:#"Hello"];
if (self) {
NSLog(#"1 :");
}
return self;
}
- (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.
}
#end
ViewController2.h
#interface ViewController2 : UIViewController
#property (nonatomic, strong) NSString *name;
-(instancetype)initWithName:(NSString *)name;
#end
ViewController2.m
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self.name = name;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2 : %#", self.name);
}
#end
I tired your code and it worked just fine. I suspect, that you don't instantiate your ViewController directly. Do you use storyboard in your project? If yes - you should override initWithCoder: in ViewController.
However, it is bad idea to set any properties of view controllers during init-family methods. According to Apple recommendations, all customisation should be done in -viewDidLoad or -viewWill/DidAppear methods.
If you absolutely in need to set name property from outside, it is better you assign it directly, not trough passing argument to init method.
One more thing - initWithNibName:bundle: is Designated initializer. It means, that your subclasses absolutely must in the end call it during initialisation chain.
Updated - 16-Mar-2016
Example :
ViewController1.h
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : ViewController2
#end
ViewController1.m
#import "ViewController1.h"
#interface ViewController1 ()
#end
#implementation ViewController1
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil andUserName:#"Ramesh Annadurai"];
// we can use above init method or below method, both will work
self = [super initWithName:#"Test Name"];
if (self) {
[self.view setBackgroundColor:[UIColor brownColor]];
}
return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
//self = [super initWithCoder:aDecoder];
// If you are using storyboard use this initWithCoder method.
self = [super initWithName:#"Test Name"];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
ViewController2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUserName:(NSString *) userName;
- (instancetype)initWithName:(NSString *) userName;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#property (strong, nonatomic) NSString *userName;
#end
#implementation ViewController2
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUserName:(NSString *) userName
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.userName = userName;
}
return self;
}
- (instancetype)initWithName:(NSString *) userName
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.userName = userName;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"User Name : %#", self.userName);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
I am trying to pass an NSString variable from one view controller to the other but I have only been able to find tutorials and information on doing this by the use of a button IBAction but and have not been able to pass it successfully. Are there any easy alternatives without using a button?
#import "DetailController.h"
#import "City.h"
#import "VideoController.h"
#interface DetailController ()
#end
#implementation DetailController
#synthesize city, ClubName, Price, Vip, Promo, remain,p,deal,money,camera,cam;
- (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.
UIFont *labelFont=[UIFont fontWithName:#"Deutsch Gothic" size:20.0];
UIFont *myFont=[UIFont fontWithName:#"Deutsch Gothic" size:30.0];
UIFont *titleFont=[UIFont fontWithName:#"Deutsch Gothic" size:40.0];
NSString * name= self.city.clubName;
NSString * line= self.city.clubLine;
NSString * description= self.city.promo;
NSString * price= self.city.price;
cam=self.city.camera;
remain.font=labelFont;
remain.text=#"VIP Remaining :";
p.font=labelFont;
p.text=#"Price :";
money.font=myFont;
deal.font=labelFont;
deal.text=#"Promotions :";
ClubName.font=titleFont;
ClubName.text=name;
Vip.font=myFont;
Vip.text=line;
Price.font=myFont;
Price.text=price;
Promo.font=labelFont;
Promo.text=description;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am trying to pass cam or camera to videoController
If you are trying to Push between view controllers than you can create a property in view controller and before pushing view controller set the value.You can do this in following way:-
In View Controller2 declare a property
#property(nonatomic,strong) NSString *passedString;
in View Controller 1 do this
ViewController2 *vc2 = [ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[vc setPassedString:#"Hello"];
[self.navigationController pushViewController:vc2 animated:YES];
I have a problem with my parse backend app.
On Parse I have a Class like this: name (string) , imageFile (File) and help (string).
Everything works great except the Images. When I tap on a cell, I am pushed to the DetailViewController the 2 Strings appear but the ImageView (PFImageView) is empty. The ImageView does not show my Images.
Maybe there is a problem with passing the image file?
Here is my code:
MechanikViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface MechanikViewController : PFQueryTableViewController
#end
MechanikViewController.m:
#import "MechanikViewController.h"
#import "MechanikDetailViewController.h"
#import "Mechanik.h"
#interface MechanikViewController ()
#end
#implementation MechanikViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
self.parseClassName = #"Mechanik";
self.textKey = #"name";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
[query orderByAscending:#"name"];
return query;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *simpleTableIdentifier = #"MechanikCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [object objectForKey:#"name"];
return cell;
}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
NSLog(#"error: %#", [error localizedDescription]);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
MechanikDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.objects objectAtIndex:indexPath.row];
Mechanik *mechanik = [[Mechanik alloc] init];
mechanik.name = [object objectForKey:#"name"];
mechanik.imageFile = [object objectForKey:#"imageFile"];
mechanik.help = [object objectForKey:#"help"];
destViewController.mechanik = mechanik;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
MechanikDetailViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#import "Mechanik.h"
#interface MechanikDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet PFImageView *formelImage;
#property (weak, nonatomic) IBOutlet UILabel *helpLabel;
#property (nonatomic, strong) Mechanik *mechanik;
#end
MechanikDetailViewController.m:
#import "MechanikDetailViewController.h"
#interface MechanikDetailViewController ()
#end
#implementation MechanikDetailViewController
#synthesize formelImage;
#synthesize helpLabel;
#synthesize mechanik;
- (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.
self.title = mechanik.name;
self.helpLabel.text = mechanik.help;
self.formelImage.file = mechanik.imageFile;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
Mechanik.h:
#import <Foundation/Foundation.h>
#import <Parse/Parse.h>
#interface Mechanik : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, strong) PFFile *imageFile;
#property (nonatomic ,strong) NSString *help;
#end
Mechanik.m:
#import "Mechanik.h"
#implementation Mechanik
#synthesize name;
#synthesize imageFile;
#synthesize help;
#end
Please help me.
Create a PFObject in DetailViewController
#property (nonatomic, strong)PFObject *object
Pass a PFObject from master to detail
PFObject *object = [self.objects objectAtIndex:indexPath.row];
destViewController.object = object;
Set data in ViewDidAppear
pfImageView.imageFile = [object objectForKey:#"imageFile"];
[pfImageView loadInBackground];
A class:
In following method, I use blocks to replace common delegate. Is it ok? The block in the didSelectRowAtIndexPath method, I use it replace common delegate, but if it run, I click the table cell and the code crashes.
typedef void (^Block)(NSString *id,NSString *cityName);
#interface WLCCityListViewController : WLCBaseSquareViewController <UITableViewDataSource,UITableViewDelegate>
{
Block _block;
id<commonDelegate>delegate;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil Block:(Block)block
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_block=block;
}
return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WLCMainViewCityListData *data=[[self citylists]objectAtIndex:[indexPath section]];
// [self.delegate seleteCityID:[[data.cityList objectAtIndex:indexPath.row]objectForKey:#"id"] CityName:[[data.cityList objectAtIndex:indexPath.row]objectForKey:#"name"]];
NSString *a1 =[[data.cityList objectAtIndex:indexPath.row]objectForKey:#"id"];
NSString *a2 =[[data.cityList objectAtIndex:indexPath.row]objectForKey:#"name"];
_block(a1,a2);
}
B class:
#interface WLCMainViewController : WLCBaseSquareViewController
{
}
#implementation WLCMainViewController
-(void)viewDidLoad {
WLCCityListViewController *tableViewController = [[[WLCCityListViewController alloc]initWithNibName:#"WLCCityListViewController" bundle:nil Block:^(NSString *id, NSString *cityName) {
self.cityID=id;
WLCMainViewModel *model=(WLCMainViewModel *)self.mainviewModel;
model.cityID=id;
[model sendRequest];
[self.view startWaiting];
}] autorelease];
}
_block = block;
This line makes the crash. you just assign a block but doesn't keep it.DON'T USE retain,only use copy.
declare a copy property.
#property (nonatomic, copy) Block _block;
and use setter. or just change you code like this.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil Block: (Block)block
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//_block=block;
_block = block? [[block copy] autorelease]: nil;
}
return self;
}
You can consider the block as a OC object.
Generally if you need to keep the block, use Block_copy to do so and don't forget to Block_release it.
The problem of your code is that the block is autoreleased when you call it.
In one.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *carNumber = [self.carNumberArrayFromPlistFile objectAtIndex:row];
NSString *engineNumber = [self.engineNumberArrayFromPlistFile objectAtIndex:row];
CarInfo *oneCarInfo = [[CarInfo alloc] initWithCarNumber:carNumber engineNumber:engineNumber];
Two *two = [[Two alloc] initWithNibName:#"Two" bundle:nil];
two.car = oneCarInfo;
[self.navigationController pushViewController:two animated:YES];
[oneCarInfo release];
[two release];
}
In two.h
#interface Two : UITabBarController
{
CarInfo *car;
}
#property (nonatomic, retain)CarInfo *car;
And why the car in Two.m is always null? Please help me with this. Thank you guys!
Two.m:
#interface Two ()
#end
#implementation Two
#synthesize car;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.car = nil;
}
- (void)dealloc
{
[super dealloc];
}
#end
#PeterPajchl : you are not supposed to push instance of UITabBarController onto the stack of UINavigationController - check the documentation.